[valid] Check that the last switch case isn't falltrough (#1485)

This commit is contained in:
João Capucho authored and GitHub committed 2021-10-26 21:33:49 -04:00
1 parent 00bbbed90a
commit 0458f0a404
1 file changed
+13
+13
View File
@@ -108,6 +108,8 @@ pub enum FunctionError {
ConflictingSwitchCase(i32),
#[error("Multiple `default` cases are present")]
MultipleDefaultCases,
#[error("The last `switch` case contains a `falltrough`")]
LastCaseFallTrough,
#[error("The pointer {0:?} doesn't relate to a valid destination for a store")]
InvalidStorePointer(Handle<crate::Expression>),
#[error("The value {0:?} can not be stored")]
@@ -469,6 +471,17 @@ impl super::Validator {
}
}
}
if let Some(case) = cases.last() {
if case.fall_through {
return Err(FunctionError::LastCaseFallTrough.with_span_static(
case.body
.span_iter()
.next()
.map_or(Default::default(), |(_, s)| *s),
"bad switch arm here",
));
}
}
let pass_through_abilities = context.abilities
& (ControlFlowAbility::RETURN | ControlFlowAbility::CONTINUE);
let sub_context =