Skip to content

Commit 96eab06

Browse files
committed
Auto merge of rust-lang#11859 - y21:issue11856, r=blyxyas
[`missing_asserts_for_indexing`]: work with bodies instead of blocks separately Fixes rust-lang#11856 Before this change, this lint would check blocks independently of each other, which means that it misses `assert!()`s from parent blocks. ```rs // check_block assert!(x.len() > 1); { // check_block // no assert here let _ = x[0] + x[1]; } ``` This PR changes it to work with bodies rather than individual blocks. That means that a function will be checked in one go and we can remember if an `assert!` occurred anywhere. Eventually it would be nice to have a more control flow-aware analysis, possibly by rewriting it as a MIR lint, but that's more complicated and I wanted this fixed first. changelog: [`missing_asserts_for_indexing`]: accept `assert!`s from parent blocks
2 parents c3243f9 + 553857b commit 96eab06

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

clippy_lints/src/missing_asserts_for_indexing.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use clippy_utils::{eq_expr_value, hash_expr, higher};
99
use rustc_ast::{LitKind, RangeLimits};
1010
use rustc_data_structures::unhash::UnhashMap;
1111
use rustc_errors::{Applicability, Diagnostic};
12-
use rustc_hir::{BinOp, Block, Expr, ExprKind, UnOp};
12+
use rustc_hir::{BinOp, Block, Body, Expr, ExprKind, UnOp};
1313
use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_session::{declare_lint_pass, declare_tool_lint};
1515
use rustc_span::source_map::Spanned;
@@ -390,10 +390,10 @@ fn report_indexes(cx: &LateContext<'_>, map: &UnhashMap<u64, Vec<IndexEntry<'_>>
390390
}
391391

392392
impl LateLintPass<'_> for MissingAssertsForIndexing {
393-
fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) {
393+
fn check_body(&mut self, cx: &LateContext<'_>, body: &Body<'_>) {
394394
let mut map = UnhashMap::default();
395395

396-
for_each_expr(block, |expr| {
396+
for_each_expr(body.value, |expr| {
397397
check_index(cx, expr, &mut map);
398398
check_assert(cx, expr, &mut map);
399399
ControlFlow::<!, ()>::Continue(())

tests/ui/missing_asserts_for_indexing_unfixable.rs

+22
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,26 @@ fn index_struct_different_fields(f: &Foo<'_>) {
4646
let _ = f.v[0] + f.v2[1];
4747
}
4848

49+
fn shadowing() {
50+
let x: &[i32] = &[1];
51+
assert!(x.len() > 1);
52+
53+
let x: &[i32] = &[1];
54+
let _ = x[0] + x[1];
55+
//~^ ERROR: indexing into a slice multiple times without an `assert`
56+
}
57+
58+
pub fn issue11856(values: &[i32]) -> usize {
59+
let mut ascending = Vec::new();
60+
for w in values.windows(2) {
61+
assert!(w.len() > 1);
62+
if w[0] < w[1] {
63+
ascending.push((w[0], w[1]));
64+
} else {
65+
ascending.push((w[1], w[0]));
66+
}
67+
}
68+
ascending.len()
69+
}
70+
4971
fn main() {}

tests/ui/missing_asserts_for_indexing_unfixable.stderr

+20-1
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,24 @@ LL | let _ = f.v[0] + f.v[1];
160160
| ^^^^^^
161161
= note: asserting the length before indexing will elide bounds checks
162162

163-
error: aborting due to 7 previous errors
163+
error: indexing into a slice multiple times without an `assert`
164+
--> $DIR/missing_asserts_for_indexing_unfixable.rs:54:13
165+
|
166+
LL | let _ = x[0] + x[1];
167+
| ^^^^^^^^^^^
168+
|
169+
= help: consider asserting the length before indexing: `assert!(x.len() > 1);`
170+
note: slice indexed here
171+
--> $DIR/missing_asserts_for_indexing_unfixable.rs:54:13
172+
|
173+
LL | let _ = x[0] + x[1];
174+
| ^^^^
175+
note: slice indexed here
176+
--> $DIR/missing_asserts_for_indexing_unfixable.rs:54:20
177+
|
178+
LL | let _ = x[0] + x[1];
179+
| ^^^^
180+
= note: asserting the length before indexing will elide bounds checks
181+
182+
error: aborting due to 8 previous errors
164183

0 commit comments

Comments
 (0)