Skip to content

Commit 4b021f6

Browse files
committed
point the suggestion at each individual span
This makes the labels redundant.
1 parent 9ced0f5 commit 4b021f6

File tree

3 files changed

+37
-438
lines changed

3 files changed

+37
-438
lines changed

clippy_lints/src/missing_asserts_for_indexing.rs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,12 @@ declare_clippy_lint! {
6767
}
6868
declare_lint_pass!(MissingAssertsForIndexing => [MISSING_ASSERTS_FOR_INDEXING]);
6969

70-
fn report_lint<F>(cx: &LateContext<'_>, full_span: Span, msg: &'static str, indexes: Vec<Span>, f: F)
70+
fn report_lint<F>(cx: &LateContext<'_>, index_spans: Vec<Span>, msg: &'static str, f: F)
7171
where
7272
F: FnOnce(&mut Diag<'_, ()>),
7373
{
74-
span_lint_and_then(cx, MISSING_ASSERTS_FOR_INDEXING, full_span, msg, |diag| {
74+
span_lint_and_then(cx, MISSING_ASSERTS_FOR_INDEXING, index_spans, msg, |diag| {
7575
f(diag);
76-
for span in indexes {
77-
diag.span_note(span, "slice indexed here");
78-
}
7976
diag.note_once("asserting the length before indexing will elide bounds checks");
8077
});
8178
}
@@ -213,15 +210,6 @@ impl<'hir> IndexEntry<'hir> {
213210
| IndexEntry::IndexWithoutAssert { slice, .. } => slice,
214211
}
215212
}
216-
217-
pub fn index_spans(&self) -> Option<&[Span]> {
218-
match self {
219-
IndexEntry::StrayAssert { .. } => None,
220-
IndexEntry::AssertWithIndex { indexes, .. } | IndexEntry::IndexWithoutAssert { indexes, .. } => {
221-
Some(indexes)
222-
},
223-
}
224-
}
225213
}
226214

227215
/// Extracts the upper index of a slice indexing expression.
@@ -357,14 +345,6 @@ fn check_assert<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut Un
357345
fn report_indexes(cx: &LateContext<'_>, map: UnindexMap<u64, Vec<IndexEntry<'_>>>) {
358346
for bucket in map.into_values() {
359347
for entry in bucket {
360-
let Some(full_span) = entry
361-
.index_spans()
362-
.and_then(|spans| spans.first().zip(spans.last()))
363-
.map(|(low, &high)| low.to(high))
364-
else {
365-
continue;
366-
};
367-
368348
match entry {
369349
IndexEntry::AssertWithIndex {
370350
highest_index,
@@ -418,9 +398,8 @@ fn report_indexes(cx: &LateContext<'_>, map: UnindexMap<u64, Vec<IndexEntry<'_>>
418398
if let Some(sugg) = sugg {
419399
report_lint(
420400
cx,
421-
full_span,
422-
"indexing into a slice multiple times with an `assert` that does not cover the highest index",
423401
indexes,
402+
"indexing into a slice multiple times with an `assert` that does not cover the highest index",
424403
|diag| {
425404
diag.span_suggestion(
426405
assert_span,
@@ -442,9 +421,8 @@ fn report_indexes(cx: &LateContext<'_>, map: UnindexMap<u64, Vec<IndexEntry<'_>>
442421
// adding an `assert!` that covers the highest index
443422
report_lint(
444423
cx,
445-
full_span,
446-
"indexing into a slice multiple times without an `assert`",
447424
indexes,
425+
"indexing into a slice multiple times without an `assert`",
448426
|diag| {
449427
diag.help(format!(
450428
"consider asserting the length before indexing: `assert!({}.len() > {highest_index});`",

0 commit comments

Comments
 (0)