Skip to content

Commit 790922c

Browse files
committed
update ui tests and some minor cleanups
1 parent b54bac9 commit 790922c

6 files changed

+94
-96
lines changed

clippy_lints/src/missing_asserts_for_indexing.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use std::mem;
22
use std::ops::ControlFlow;
33

4-
use clippy_utils::eq_expr_value;
4+
use clippy_utils::comparisons::{normalize_comparison, Rel};
5+
use clippy_utils::diagnostics::span_lint_and_then;
56
use clippy_utils::source::snippet;
6-
use clippy_utils::{
7-
comparisons::{normalize_comparison, Rel},
8-
diagnostics::span_lint_and_then,
9-
hash_expr, higher,
10-
visitors::for_each_expr,
11-
};
7+
use clippy_utils::visitors::for_each_expr;
8+
use clippy_utils::{eq_expr_value, hash_expr, higher};
129
use rustc_ast::{LitKind, RangeLimits};
1310
use rustc_data_structures::unhash::UnhashMap;
1411
use rustc_errors::{Applicability, Diagnostic};
@@ -220,13 +217,13 @@ fn upper_index_expr(expr: &Expr<'_>) -> Option<usize> {
220217

221218
/// Checks if the expression is an index into a slice and adds it to `indexes`
222219
fn check_index<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut UnhashMap<u64, Vec<IndexEntry<'hir>>>) {
223-
if let ExprKind::Index(slice, index_lit) = expr.kind
220+
if let ExprKind::Index(slice, index_lit, _) = expr.kind
224221
&& cx.typeck_results().expr_ty_adjusted(slice).peel_refs().is_slice()
225222
&& let Some(index) = upper_index_expr(index_lit)
226223
{
227224
let hash = hash_expr(cx, slice);
228225

229-
let indexes = map.entry(hash).or_insert_with(Vec::new);
226+
let indexes = map.entry(hash).or_default();
230227
let entry = indexes.iter_mut().find(|entry| eq_expr_value(cx, entry.slice(), slice));
231228

232229
if let Some(entry) = entry {
@@ -261,7 +258,7 @@ fn check_index<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut Unh
261258
fn check_assert<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut UnhashMap<u64, Vec<IndexEntry<'hir>>>) {
262259
if let Some((comparison, asserted_len, slice)) = assert_len_expr(cx, expr) {
263260
let hash = hash_expr(cx, slice);
264-
let indexes = map.entry(hash).or_insert_with(Vec::new);
261+
let indexes = map.entry(hash).or_default();
265262

266263
let entry = indexes.iter_mut().find(|entry| eq_expr_value(cx, entry.slice(), slice));
267264

@@ -301,9 +298,10 @@ fn report_indexes(cx: &LateContext<'_>, map: &UnhashMap<u64, Vec<IndexEntry<'_>>
301298
let Some(full_span) = entry
302299
.index_spans()
303300
.and_then(|spans| spans.first().zip(spans.last()))
304-
.map(|(low, &high)| low.to(high)) else {
305-
continue;
306-
};
301+
.map(|(low, &high)| low.to(high))
302+
else {
303+
continue;
304+
};
307305

308306
match entry {
309307
IndexEntry::AssertWithIndex {

tests/ui/missing_asserts_for_indexing.fixed

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@run-rustfix
2-
31
#![allow(unused)]
42
#![warn(clippy::missing_asserts_for_indexing)]
53

@@ -29,26 +27,26 @@ fn sum_with_assert_ge_other_way(v: &[u8]) -> u8 {
2927

3028
fn sum_with_assert_lt(v: &[u8]) -> u8 {
3129
assert!(v.len() > 4);
32-
//~^ ERROR incorrect length
3330
v[0] + v[1] + v[2] + v[3] + v[4]
31+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
3432
}
3533

3634
fn sum_with_assert_le(v: &[u8]) -> u8 {
3735
assert!(v.len() > 4);
38-
//~^ ERROR incorrect length
3936
v[0] + v[1] + v[2] + v[3] + v[4]
37+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
4038
}
4139

4240
fn sum_with_incorrect_assert_len(v: &[u8]) -> u8 {
4341
assert!(v.len() > 4);
44-
//~^ ERROR incorrect length
4542
v[0] + v[1] + v[2] + v[3] + v[4]
43+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
4644
}
4745

4846
fn sum_with_incorrect_assert_len2(v: &[u8]) -> u8 {
4947
assert!(v.len() > 4);
50-
//~^ ERROR incorrect length
5148
v[0] + v[1] + v[2] + v[3] + v[4]
49+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
5250
}
5351

5452
// ok, don't lint for single array access
@@ -65,8 +63,8 @@ fn subslice_ok(v: &[u8]) {
6563

6664
fn subslice_bad(v: &[u8]) {
6765
assert!(v.len() > 3);
68-
//~^ ERROR incorrect length
6966
let _ = v[0];
67+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
7068
let _ = v[1..4];
7169
}
7270

@@ -79,8 +77,8 @@ fn subslice_inclusive_ok(v: &[u8]) {
7977

8078
fn subslice_inclusive_bad(v: &[u8]) {
8179
assert!(v.len() > 4);
82-
//~^ ERROR incorrect length
8380
let _ = v[0];
81+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
8482
let _ = v[1..=4];
8583
}
8684

@@ -93,17 +91,17 @@ fn index_different_slices_ok(v1: &[u8], v2: &[u8]) {
9391

9492
fn index_different_slices_wrong_len(v1: &[u8], v2: &[u8]) {
9593
assert!(v1.len() > 12);
96-
//~^ ERROR incorrect length
9794
assert!(v2.len() > 15);
98-
//~^ ERROR incorrect length
9995
let _ = v1[0] + v1[12];
96+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
10097
let _ = v2[5] + v2[15];
98+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
10199
}
102100
fn index_different_slices_one_wrong_len(v1: &[u8], v2: &[u8]) {
103101
assert!(v1.len() > 12);
104-
//~^ ERROR incorrect length
105102
assert!(v2.len() > 15);
106103
let _ = v1[0] + v1[12];
104+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
107105
let _ = v2[5] + v2[15];
108106
}
109107

tests/ui/missing_asserts_for_indexing.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//@run-rustfix
2-
31
#![allow(unused)]
42
#![warn(clippy::missing_asserts_for_indexing)]
53

@@ -29,26 +27,26 @@ fn sum_with_assert_ge_other_way(v: &[u8]) -> u8 {
2927

3028
fn sum_with_assert_lt(v: &[u8]) -> u8 {
3129
assert!(v.len() < 5);
32-
//~^ ERROR incorrect length
3330
v[0] + v[1] + v[2] + v[3] + v[4]
31+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
3432
}
3533

3634
fn sum_with_assert_le(v: &[u8]) -> u8 {
3735
assert!(v.len() <= 5);
38-
//~^ ERROR incorrect length
3936
v[0] + v[1] + v[2] + v[3] + v[4]
37+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
4038
}
4139

4240
fn sum_with_incorrect_assert_len(v: &[u8]) -> u8 {
4341
assert!(v.len() > 3);
44-
//~^ ERROR incorrect length
4542
v[0] + v[1] + v[2] + v[3] + v[4]
43+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
4644
}
4745

4846
fn sum_with_incorrect_assert_len2(v: &[u8]) -> u8 {
4947
assert!(v.len() >= 4);
50-
//~^ ERROR incorrect length
5148
v[0] + v[1] + v[2] + v[3] + v[4]
49+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
5250
}
5351

5452
// ok, don't lint for single array access
@@ -65,8 +63,8 @@ fn subslice_ok(v: &[u8]) {
6563

6664
fn subslice_bad(v: &[u8]) {
6765
assert!(v.len() >= 3);
68-
//~^ ERROR incorrect length
6966
let _ = v[0];
67+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
7068
let _ = v[1..4];
7169
}
7270

@@ -79,8 +77,8 @@ fn subslice_inclusive_ok(v: &[u8]) {
7977

8078
fn subslice_inclusive_bad(v: &[u8]) {
8179
assert!(v.len() >= 4);
82-
//~^ ERROR incorrect length
8380
let _ = v[0];
81+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
8482
let _ = v[1..=4];
8583
}
8684

@@ -93,17 +91,17 @@ fn index_different_slices_ok(v1: &[u8], v2: &[u8]) {
9391

9492
fn index_different_slices_wrong_len(v1: &[u8], v2: &[u8]) {
9593
assert!(v1.len() >= 12);
96-
//~^ ERROR incorrect length
9794
assert!(v2.len() >= 15);
98-
//~^ ERROR incorrect length
9995
let _ = v1[0] + v1[12];
96+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
10097
let _ = v2[5] + v2[15];
98+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
10199
}
102100
fn index_different_slices_one_wrong_len(v1: &[u8], v2: &[u8]) {
103101
assert!(v1.len() >= 12);
104-
//~^ ERROR incorrect length
105102
assert!(v2.len() > 15);
106103
let _ = v1[0] + v1[12];
104+
//~^ ERROR: indexing into a slice multiple times with an `assert` that does not cover the
107105
let _ = v2[5] + v2[15];
108106
}
109107

0 commit comments

Comments
 (0)