Skip to content

Commit bc76aef

Browse files
committed
fix: adjust the applicability for the suggestion
1 parent 2379120 commit bc76aef

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

clippy_lints/src/missing_asserts_for_indexing.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::comparisons::{Rel, normalize_comparison};
55
use clippy_utils::diagnostics::span_lint_and_then;
66
use clippy_utils::higher::{If, Range};
77
use clippy_utils::macros::{find_assert_eq_args, first_node_macro_backtrace, root_macro_call};
8-
use clippy_utils::source::snippet;
8+
use clippy_utils::source::{snippet, snippet_with_applicability};
99
use clippy_utils::visitors::for_each_expr_without_closures;
1010
use clippy_utils::{eq_expr_value, hash_expr};
1111
use rustc_ast::{BinOpKind, LitKind, RangeLimits};
@@ -357,41 +357,33 @@ fn report_indexes(cx: &LateContext<'_>, map: UnindexMap<u64, Vec<IndexEntry<'_>>
357357
slice,
358358
macro_call,
359359
} if indexes.len() > 1 && !is_first_highest => {
360+
let mut app = Applicability::MachineApplicable;
361+
let slice_str = snippet_with_applicability(cx, slice.span, "_", &mut app);
360362
// if we have found an `assert!`, let's also check that it's actually right
361363
// and if it covers the highest index and if not, suggest the correct length
362364
let sugg = match comparison {
363365
// `v.len() < 5` and `v.len() <= 5` does nothing in terms of bounds checks.
364366
// The user probably meant `v.len() > 5`
365-
LengthComparison::LengthLessThanInt | LengthComparison::LengthLessThanOrEqualInt => Some(
366-
format!("assert!({}.len() > {highest_index})", snippet(cx, slice.span, "..")),
367-
),
367+
LengthComparison::LengthLessThanInt | LengthComparison::LengthLessThanOrEqualInt => {
368+
Some(format!("assert!({slice_str}.len() > {highest_index})",))
369+
},
368370
// `5 < v.len()` == `v.len() > 5`
369-
LengthComparison::IntLessThanLength if asserted_len < highest_index => Some(format!(
370-
"assert!({}.len() > {highest_index})",
371-
snippet(cx, slice.span, "..")
372-
)),
371+
LengthComparison::IntLessThanLength if asserted_len < highest_index => {
372+
Some(format!("assert!({slice_str}.len() > {highest_index})",))
373+
},
373374
// `5 <= v.len() == `v.len() >= 5`
374-
LengthComparison::IntLessThanOrEqualLength if asserted_len <= highest_index => Some(format!(
375-
"assert!({}.len() > {highest_index})",
376-
snippet(cx, slice.span, "..")
377-
)),
375+
LengthComparison::IntLessThanOrEqualLength if asserted_len <= highest_index => {
376+
Some(format!("assert!({slice_str}.len() > {highest_index})",))
377+
},
378378
// `highest_index` here is rather a length, so we need to add 1 to it
379379
LengthComparison::LengthEqualInt if asserted_len < highest_index + 1 => match macro_call {
380-
sym::assert_eq_macro => Some(format!(
381-
"assert_eq!({}.len(), {})",
382-
snippet(cx, slice.span, ".."),
383-
highest_index + 1
384-
)),
385-
sym::debug_assert_eq_macro => Some(format!(
386-
"debug_assert_eq!({}.len(), {})",
387-
snippet(cx, slice.span, ".."),
388-
highest_index + 1
389-
)),
390-
_ => Some(format!(
391-
"assert!({}.len() == {})",
392-
snippet(cx, slice.span, ".."),
393-
highest_index + 1
394-
)),
380+
sym::assert_eq_macro => {
381+
Some(format!("assert_eq!({slice_str}.len(), {})", highest_index + 1))
382+
},
383+
sym::debug_assert_eq_macro => {
384+
Some(format!("debug_assert_eq!({slice_str}.len(), {})", highest_index + 1))
385+
},
386+
_ => Some(format!("assert!({slice_str}.len() == {})", highest_index + 1)),
395387
},
396388
_ => None,
397389
};
@@ -406,7 +398,7 @@ fn report_indexes(cx: &LateContext<'_>, map: UnindexMap<u64, Vec<IndexEntry<'_>>
406398
assert_span,
407399
"provide the highest index that is indexed with",
408400
sugg,
409-
Applicability::MachineApplicable,
401+
app,
410402
);
411403
},
412404
);

0 commit comments

Comments
 (0)