From 4ca89bca3e2c686a2d8a638ed752d5099d0f61b4 Mon Sep 17 00:00:00 2001 From: Bogdan Arabadzhi <8407846+barabadzhi@users.noreply.github.com> Date: Sun, 14 Jan 2024 13:05:28 +0100 Subject: [PATCH] Skip lint for assertion evaluated to false --- compiler/rustc_mir_transform/src/const_prop_lint.rs | 8 +++++++- tests/ui/const_prop/issue-119908.rs | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/ui/const_prop/issue-119908.rs diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index d0bbca08a4068..f014852f39fc6 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -456,9 +456,15 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { let value = &self.eval_operand(cond, location)?; trace!("assertion on {:?} should be {:?}", value, expected); - let expected = Scalar::from_bool(expected); let value_const = self.use_ecx(location, |this| this.ecx.read_scalar(value))?; + if value_const == Scalar::from_bool(false) { + // Skip the assertion if the condition is evaluated to false. + return None; + } + + let expected = Scalar::from_bool(expected); + if expected != value_const { // Poison all places this operand references so that further code // doesn't use the invalid value diff --git a/tests/ui/const_prop/issue-119908.rs b/tests/ui/const_prop/issue-119908.rs new file mode 100644 index 0000000000000..ecb297709de65 --- /dev/null +++ b/tests/ui/const_prop/issue-119908.rs @@ -0,0 +1,7 @@ +// run-pass +fn main() { + let x: [i32; 0] = []; + if x.len() > 0 { + x[0]; + } +}