refactor(missing_asserts_for_indexing): overhaul diagnostics (#16120)

changelog: [`missing_asserts_for_indexing`]: overhaul diagnostics
This commit is contained in:
Jason Newcomb 2025-12-08 13:34:15 +00:00 committed by GitHub
commit d5d644f400
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 114 additions and 494 deletions

View file

@ -5,7 +5,7 @@ use clippy_utils::comparisons::{Rel, normalize_comparison};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::higher::{If, Range};
use clippy_utils::macros::{find_assert_eq_args, first_node_macro_backtrace, root_macro_call};
use clippy_utils::source::snippet;
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::visitors::for_each_expr_without_closures;
use clippy_utils::{eq_expr_value, hash_expr};
use rustc_ast::{BinOpKind, LitKind, RangeLimits};
@ -67,16 +67,13 @@ declare_clippy_lint! {
}
declare_lint_pass!(MissingAssertsForIndexing => [MISSING_ASSERTS_FOR_INDEXING]);
fn report_lint<F>(cx: &LateContext<'_>, full_span: Span, msg: &'static str, indexes: &[Span], f: F)
fn report_lint<F>(cx: &LateContext<'_>, index_spans: Vec<Span>, msg: &'static str, f: F)
where
F: FnOnce(&mut Diag<'_, ()>),
{
span_lint_and_then(cx, MISSING_ASSERTS_FOR_INDEXING, full_span, msg, |diag| {
span_lint_and_then(cx, MISSING_ASSERTS_FOR_INDEXING, index_spans, msg, |diag| {
f(diag);
for span in indexes {
diag.span_note(*span, "slice indexed here");
}
diag.note("asserting the length before indexing will elide bounds checks");
diag.note_once("asserting the length before indexing will elide bounds checks");
});
}
@ -213,15 +210,6 @@ impl<'hir> IndexEntry<'hir> {
| IndexEntry::IndexWithoutAssert { slice, .. } => slice,
}
}
pub fn index_spans(&self) -> Option<&[Span]> {
match self {
IndexEntry::StrayAssert { .. } => None,
IndexEntry::AssertWithIndex { indexes, .. } | IndexEntry::IndexWithoutAssert { indexes, .. } => {
Some(indexes)
},
}
}
}
/// Extracts the upper index of a slice indexing expression.
@ -354,63 +342,47 @@ fn check_assert<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut Un
/// Inspects indexes and reports lints.
///
/// Called at the end of this lint after all indexing and `assert!` expressions have been collected.
fn report_indexes(cx: &LateContext<'_>, map: &UnindexMap<u64, Vec<IndexEntry<'_>>>) {
for bucket in map.values() {
fn report_indexes(cx: &LateContext<'_>, map: UnindexMap<u64, Vec<IndexEntry<'_>>>) {
for bucket in map.into_values() {
for entry in bucket {
let Some(full_span) = entry
.index_spans()
.and_then(|spans| spans.first().zip(spans.last()))
.map(|(low, &high)| low.to(high))
else {
continue;
};
match *entry {
match entry {
IndexEntry::AssertWithIndex {
highest_index,
is_first_highest,
asserted_len,
ref indexes,
indexes,
comparison,
assert_span,
slice,
macro_call,
} if indexes.len() > 1 && !is_first_highest => {
let mut app = Applicability::MachineApplicable;
let slice_str = snippet_with_applicability(cx, slice.span, "_", &mut app);
// if we have found an `assert!`, let's also check that it's actually right
// and if it covers the highest index and if not, suggest the correct length
let sugg = match comparison {
// `v.len() < 5` and `v.len() <= 5` does nothing in terms of bounds checks.
// The user probably meant `v.len() > 5`
LengthComparison::LengthLessThanInt | LengthComparison::LengthLessThanOrEqualInt => Some(
format!("assert!({}.len() > {highest_index})", snippet(cx, slice.span, "..")),
),
LengthComparison::LengthLessThanInt | LengthComparison::LengthLessThanOrEqualInt => {
Some(format!("assert!({slice_str}.len() > {highest_index})",))
},
// `5 < v.len()` == `v.len() > 5`
LengthComparison::IntLessThanLength if asserted_len < highest_index => Some(format!(
"assert!({}.len() > {highest_index})",
snippet(cx, slice.span, "..")
)),
LengthComparison::IntLessThanLength if asserted_len < highest_index => {
Some(format!("assert!({slice_str}.len() > {highest_index})",))
},
// `5 <= v.len() == `v.len() >= 5`
LengthComparison::IntLessThanOrEqualLength if asserted_len <= highest_index => Some(format!(
"assert!({}.len() > {highest_index})",
snippet(cx, slice.span, "..")
)),
LengthComparison::IntLessThanOrEqualLength if asserted_len <= highest_index => {
Some(format!("assert!({slice_str}.len() > {highest_index})",))
},
// `highest_index` here is rather a length, so we need to add 1 to it
LengthComparison::LengthEqualInt if asserted_len < highest_index + 1 => match macro_call {
sym::assert_eq_macro => Some(format!(
"assert_eq!({}.len(), {})",
snippet(cx, slice.span, ".."),
highest_index + 1
)),
sym::debug_assert_eq_macro => Some(format!(
"debug_assert_eq!({}.len(), {})",
snippet(cx, slice.span, ".."),
highest_index + 1
)),
_ => Some(format!(
"assert!({}.len() == {})",
snippet(cx, slice.span, ".."),
highest_index + 1
)),
sym::assert_eq_macro => {
Some(format!("assert_eq!({slice_str}.len(), {})", highest_index + 1))
},
sym::debug_assert_eq_macro => {
Some(format!("debug_assert_eq!({slice_str}.len(), {})", highest_index + 1))
},
_ => Some(format!("assert!({slice_str}.len() == {})", highest_index + 1)),
},
_ => None,
};
@ -418,22 +390,21 @@ fn report_indexes(cx: &LateContext<'_>, map: &UnindexMap<u64, Vec<IndexEntry<'_>
if let Some(sugg) = sugg {
report_lint(
cx,
full_span,
"indexing into a slice multiple times with an `assert` that does not cover the highest index",
indexes,
"indexing into a slice multiple times with an `assert` that does not cover the highest index",
|diag| {
diag.span_suggestion(
diag.span_suggestion_verbose(
assert_span,
"provide the highest index that is indexed with",
sugg,
Applicability::MachineApplicable,
app,
);
},
);
}
},
IndexEntry::IndexWithoutAssert {
ref indexes,
indexes,
highest_index,
is_first_highest,
slice,
@ -442,9 +413,8 @@ fn report_indexes(cx: &LateContext<'_>, map: &UnindexMap<u64, Vec<IndexEntry<'_>
// adding an `assert!` that covers the highest index
report_lint(
cx,
full_span,
"indexing into a slice multiple times without an `assert`",
indexes,
"indexing into a slice multiple times without an `assert`",
|diag| {
diag.help(format!(
"consider asserting the length before indexing: `assert!({}.len() > {highest_index});`",
@ -469,6 +439,6 @@ impl LateLintPass<'_> for MissingAssertsForIndexing {
ControlFlow::<!, ()>::Continue(())
});
report_indexes(cx, &map);
report_indexes(cx, map);
}
}

View file

@ -1,407 +1,191 @@
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:30:5
|
LL | assert!(v.len() < 5);
| -------------------- help: provide the highest index that is indexed with: `assert!(v.len() > 4)`
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^ ^^^^ ^^^^ ^^^^ ^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:30:5
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:30:12
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:30:19
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:30:26
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:30:33
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
= note: asserting the length before indexing will elide bounds checks
= note: `-D clippy::missing-asserts-for-indexing` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::missing_asserts_for_indexing)]`
help: provide the highest index that is indexed with
|
LL - assert!(v.len() < 5);
LL + assert!(v.len() > 4);
|
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:36:5
|
LL | assert!(v.len() <= 5);
| --------------------- help: provide the highest index that is indexed with: `assert!(v.len() > 4)`
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^ ^^^^ ^^^^ ^^^^ ^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:36:5
help: provide the highest index that is indexed with
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:36:12
LL - assert!(v.len() <= 5);
LL + assert!(v.len() > 4);
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:36:19
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:36:26
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:36:33
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:42:5
|
LL | assert!(v.len() > 3);
| -------------------- help: provide the highest index that is indexed with: `assert!(v.len() > 4)`
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^ ^^^^ ^^^^ ^^^^ ^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:42:5
help: provide the highest index that is indexed with
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:42:12
LL - assert!(v.len() > 3);
LL + assert!(v.len() > 4);
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:42:19
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:42:26
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:42:33
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:48:5
|
LL | assert!(v.len() >= 4);
| --------------------- help: provide the highest index that is indexed with: `assert!(v.len() > 4)`
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^ ^^^^ ^^^^ ^^^^ ^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:48:5
help: provide the highest index that is indexed with
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:48:12
LL - assert!(v.len() >= 4);
LL + assert!(v.len() > 4);
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:48:19
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:48:26
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:48:33
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:66:13
|
LL | assert!(v.len() >= 3);
| --------------------- help: provide the highest index that is indexed with: `assert!(v.len() > 3)`
LL | let _ = v[0];
| _____________^
... |
LL | | let _ = v[1..4];
| |___________________^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:66:13
|
LL | let _ = v[0];
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:69:13
|
...
LL | let _ = v[1..4];
| ^^^^^^^
= note: asserting the length before indexing will elide bounds checks
|
help: provide the highest index that is indexed with
|
LL - assert!(v.len() >= 3);
LL + assert!(v.len() > 3);
|
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:81:13
|
LL | assert!(v.len() >= 4);
| --------------------- help: provide the highest index that is indexed with: `assert!(v.len() > 4)`
LL | let _ = v[0];
| _____________^
... |
LL | | let _ = v[1..=4];
| |____________________^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:81:13
|
LL | let _ = v[0];
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:84:13
|
...
LL | let _ = v[1..=4];
| ^^^^^^^^
= note: asserting the length before indexing will elide bounds checks
|
help: provide the highest index that is indexed with
|
LL - assert!(v.len() >= 4);
LL + assert!(v.len() > 4);
|
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:97:13
|
LL | assert!(v1.len() >= 12);
| ----------------------- help: provide the highest index that is indexed with: `assert!(v1.len() > 12)`
LL | assert!(v2.len() >= 15);
LL | let _ = v1[0] + v1[12];
| ^^^^^^^^^^^^^^
| ^^^^^ ^^^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:97:13
help: provide the highest index that is indexed with
|
LL | let _ = v1[0] + v1[12];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:97:21
LL - assert!(v1.len() >= 12);
LL + assert!(v1.len() > 12);
|
LL | let _ = v1[0] + v1[12];
| ^^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:100:13
|
LL | assert!(v2.len() >= 15);
| ----------------------- help: provide the highest index that is indexed with: `assert!(v2.len() > 15)`
...
LL | let _ = v2[5] + v2[15];
| ^^^^^^^^^^^^^^
| ^^^^^ ^^^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:100:13
help: provide the highest index that is indexed with
|
LL | let _ = v2[5] + v2[15];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:100:21
LL - assert!(v2.len() >= 15);
LL + assert!(v2.len() > 15);
|
LL | let _ = v2[5] + v2[15];
| ^^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:106:13
|
LL | assert!(v1.len() >= 12);
| ----------------------- help: provide the highest index that is indexed with: `assert!(v1.len() > 12)`
LL | assert!(v2.len() > 15);
LL | let _ = v1[0] + v1[12];
| ^^^^^^^^^^^^^^
| ^^^^^ ^^^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:106:13
help: provide the highest index that is indexed with
|
LL | let _ = v1[0] + v1[12];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:106:21
LL - assert!(v1.len() >= 12);
LL + assert!(v1.len() > 12);
|
LL | let _ = v1[0] + v1[12];
| ^^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:131:13
|
LL | assert!(v1.len() == 2);
| ---------------------- help: provide the highest index that is indexed with: `assert!(v1.len() == 3)`
...
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^ ^^^^^ ^^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:131:13
help: provide the highest index that is indexed with
|
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:131:21
LL - assert!(v1.len() == 2);
LL + assert!(v1.len() == 3);
|
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:131:29
|
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:136:13
|
LL | assert!(2 == v3.len());
| ---------------------- help: provide the highest index that is indexed with: `assert!(v3.len() == 3)`
...
LL | let _ = v3[0] + v3[1] + v3[2];
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^ ^^^^^ ^^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:136:13
help: provide the highest index that is indexed with
|
LL | let _ = v3[0] + v3[1] + v3[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:136:21
LL - assert!(2 == v3.len());
LL + assert!(v3.len() == 3);
|
LL | let _ = v3[0] + v3[1] + v3[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:136:29
|
LL | let _ = v3[0] + v3[1] + v3[2];
| ^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:158:13
|
LL | assert_eq!(v1.len(), 2);
| ----------------------- help: provide the highest index that is indexed with: `assert_eq!(v1.len(), 3)`
...
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^ ^^^^^ ^^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:158:13
help: provide the highest index that is indexed with
|
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:158:21
LL - assert_eq!(v1.len(), 2);
LL + assert_eq!(v1.len(), 3);
|
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:158:29
|
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:163:13
|
LL | assert_eq!(2, v3.len());
| ----------------------- help: provide the highest index that is indexed with: `assert_eq!(v3.len(), 3)`
...
LL | let _ = v3[0] + v3[1] + v3[2];
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^ ^^^^^ ^^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:163:13
help: provide the highest index that is indexed with
|
LL | let _ = v3[0] + v3[1] + v3[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:163:21
LL - assert_eq!(2, v3.len());
LL + assert_eq!(v3.len(), 3);
|
LL | let _ = v3[0] + v3[1] + v3[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:163:29
|
LL | let _ = v3[0] + v3[1] + v3[2];
| ^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:172:17
|
LL | assert_eq!(v.len(), 2);
| ---------------------- help: provide the highest index that is indexed with: `assert_eq!(v.len(), 3)`
LL | let _ = v[0] + v[1] + v[2];
| ^^^^^^^^^^^^^^^^^^
| ^^^^ ^^^^ ^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:172:17
help: provide the highest index that is indexed with
|
LL | let _ = v[0] + v[1] + v[2];
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:172:24
LL - assert_eq!(v.len(), 2);
LL + assert_eq!(v.len(), 3);
|
LL | let _ = v[0] + v[1] + v[2];
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:172:31
|
LL | let _ = v[0] + v[1] + v[2];
| ^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times with an `assert` that does not cover the highest index
--> tests/ui/missing_asserts_for_indexing.rs:178:17
|
LL | debug_assert_eq!(v.len(), 2);
| ---------------------------- help: provide the highest index that is indexed with: `debug_assert_eq!(v.len(), 3)`
LL | let _ = v[0] + v[1] + v[2];
| ^^^^^^^^^^^^^^^^^^
| ^^^^ ^^^^ ^^^^
|
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:178:17
help: provide the highest index that is indexed with
|
LL | let _ = v[0] + v[1] + v[2];
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:178:24
LL - debug_assert_eq!(v.len(), 2);
LL + debug_assert_eq!(v.len(), 3);
|
LL | let _ = v[0] + v[1] + v[2];
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing.rs:178:31
|
LL | let _ = v[0] + v[1] + v[2];
| ^^^^
= note: asserting the length before indexing will elide bounds checks
error: aborting due to 15 previous errors

View file

@ -2,34 +2,9 @@ error: indexing into a slice multiple times without an `assert`
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:5:5
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^ ^^^^ ^^^^ ^^^^ ^^^^
|
= help: consider asserting the length before indexing: `assert!(v.len() > 4);`
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:5:5
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:5:12
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:5:19
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:5:26
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:5:33
|
LL | v[0] + v[1] + v[2] + v[3] + v[4]
| ^^^^
= note: asserting the length before indexing will elide bounds checks
= note: `-D clippy::missing-asserts-for-indexing` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::missing_asserts_for_indexing)]`
@ -37,191 +12,82 @@ LL | v[0] + v[1] + v[2] + v[3] + v[4]
error: indexing into a slice multiple times without an `assert`
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:10:13
|
LL | let _ = v[0];
| _____________^
... |
LL | | let _ = v[1..4];
| |___________________^
|
= help: consider asserting the length before indexing: `assert!(v.len() > 3);`
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:10:13
|
LL | let _ = v[0];
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:13:13
|
...
LL | let _ = v[1..4];
| ^^^^^^^
= note: asserting the length before indexing will elide bounds checks
|
= help: consider asserting the length before indexing: `assert!(v.len() > 3);`
error: indexing into a slice multiple times without an `assert`
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:17:13
|
LL | let a = v[0];
| _____________^
LL | |
LL | |
LL | | let b = v[1];
LL | | let c = v[2];
| |________________^
|
= help: consider asserting the length before indexing: `assert!(v.len() > 2);`
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:17:13
|
LL | let a = v[0];
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:20:13
|
...
LL | let b = v[1];
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:21:13
|
LL | let c = v[2];
| ^^^^
= note: asserting the length before indexing will elide bounds checks
|
= help: consider asserting the length before indexing: `assert!(v.len() > 2);`
error: indexing into a slice multiple times without an `assert`
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:26:13
|
LL | let _ = v1[0] + v1[12];
| ^^^^^^^^^^^^^^
| ^^^^^ ^^^^^^
|
= help: consider asserting the length before indexing: `assert!(v1.len() > 12);`
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:26:13
|
LL | let _ = v1[0] + v1[12];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:26:21
|
LL | let _ = v1[0] + v1[12];
| ^^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times without an `assert`
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:28:13
|
LL | let _ = v2[5] + v2[15];
| ^^^^^^^^^^^^^^
| ^^^^^ ^^^^^^
|
= help: consider asserting the length before indexing: `assert!(v2.len() > 15);`
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:28:13
|
LL | let _ = v2[5] + v2[15];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:28:21
|
LL | let _ = v2[5] + v2[15];
| ^^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times without an `assert`
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:35:13
|
LL | let _ = v2[5] + v2[15];
| ^^^^^^^^^^^^^^
| ^^^^^ ^^^^^^
|
= help: consider asserting the length before indexing: `assert!(v2.len() > 15);`
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:35:13
|
LL | let _ = v2[5] + v2[15];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:35:21
|
LL | let _ = v2[5] + v2[15];
| ^^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times without an `assert`
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:45:13
|
LL | let _ = f.v[0] + f.v[1];
| ^^^^^^^^^^^^^^^
| ^^^^^^ ^^^^^^
|
= help: consider asserting the length before indexing: `assert!(f.v.len() > 1);`
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:45:13
|
LL | let _ = f.v[0] + f.v[1];
| ^^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:45:22
|
LL | let _ = f.v[0] + f.v[1];
| ^^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times without an `assert`
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:59:13
|
LL | let _ = x[0] + x[1];
| ^^^^^^^^^^^
| ^^^^ ^^^^
|
= help: consider asserting the length before indexing: `assert!(x.len() > 1);`
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:59:13
|
LL | let _ = x[0] + x[1];
| ^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:59:20
|
LL | let _ = x[0] + x[1];
| ^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times without an `assert`
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:77:13
|
LL | let _ = v1[1] + v1[2];
| ^^^^^^^^^^^^^
| ^^^^^ ^^^^^
|
= help: consider asserting the length before indexing: `assert!(v1.len() > 2);`
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:77:13
|
LL | let _ = v1[1] + v1[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:77:21
|
LL | let _ = v1[1] + v1[2];
| ^^^^^
= note: asserting the length before indexing will elide bounds checks
error: indexing into a slice multiple times without an `assert`
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:85:13
|
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^ ^^^^^ ^^^^^
|
= help: consider asserting the length before indexing: `assert!(v1.len() > 2);`
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:85:13
|
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:85:21
|
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^
note: slice indexed here
--> tests/ui/missing_asserts_for_indexing_unfixable.rs:85:29
|
LL | let _ = v1[0] + v1[1] + v1[2];
| ^^^^^
= note: asserting the length before indexing will elide bounds checks
error: aborting due to 10 previous errors