Skip to content

Commit 129a39d

Browse files
committed
fix: adjust the applicability for the suggestion
1 parent f3b905d commit 129a39d

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};
@@ -356,41 +356,33 @@ fn report_indexes(cx: &LateContext<'_>, map: UnindexMap<u64, Vec<IndexEntry<'_>>
356356
slice,
357357
macro_call,
358358
} if indexes.len() > 1 && !is_first_highest => {
359+
let mut app = Applicability::MachineApplicable;
360+
let slice_str = snippet_with_applicability(cx, slice.span, "_", &mut app);
359361
// if we have found an `assert!`, let's also check that it's actually right
360362
// and if it covers the highest index and if not, suggest the correct length
361363
let sugg = match comparison {
362364
// `v.len() < 5` and `v.len() <= 5` does nothing in terms of bounds checks.
363365
// The user probably meant `v.len() > 5`
364-
LengthComparison::LengthLessThanInt | LengthComparison::LengthLessThanOrEqualInt => Some(
365-
format!("assert!({}.len() > {highest_index})", snippet(cx, slice.span, "..")),
366-
),
366+
LengthComparison::LengthLessThanInt | LengthComparison::LengthLessThanOrEqualInt => {
367+
Some(format!("assert!({slice_str}.len() > {highest_index})",))
368+
},
367369
// `5 < v.len()` == `v.len() > 5`
368-
LengthComparison::IntLessThanLength if asserted_len < highest_index => Some(format!(
369-
"assert!({}.len() > {highest_index})",
370-
snippet(cx, slice.span, "..")
371-
)),
370+
LengthComparison::IntLessThanLength if asserted_len < highest_index => {
371+
Some(format!("assert!({slice_str}.len() > {highest_index})",))
372+
},
372373
// `5 <= v.len() == `v.len() >= 5`
373-
LengthComparison::IntLessThanOrEqualLength if asserted_len <= highest_index => Some(format!(
374-
"assert!({}.len() > {highest_index})",
375-
snippet(cx, slice.span, "..")
376-
)),
374+
LengthComparison::IntLessThanOrEqualLength if asserted_len <= highest_index => {
375+
Some(format!("assert!({slice_str}.len() > {highest_index})",))
376+
},
377377
// `highest_index` here is rather a length, so we need to add 1 to it
378378
LengthComparison::LengthEqualInt if asserted_len < highest_index + 1 => match macro_call {
379-
sym::assert_eq_macro => Some(format!(
380-
"assert_eq!({}.len(), {})",
381-
snippet(cx, slice.span, ".."),
382-
highest_index + 1
383-
)),
384-
sym::debug_assert_eq_macro => Some(format!(
385-
"debug_assert_eq!({}.len(), {})",
386-
snippet(cx, slice.span, ".."),
387-
highest_index + 1
388-
)),
389-
_ => Some(format!(
390-
"assert!({}.len() == {})",
391-
snippet(cx, slice.span, ".."),
392-
highest_index + 1
393-
)),
379+
sym::assert_eq_macro => {
380+
Some(format!("assert_eq!({slice_str}.len(), {})", highest_index + 1))
381+
},
382+
sym::debug_assert_eq_macro => {
383+
Some(format!("debug_assert_eq!({slice_str}.len(), {})", highest_index + 1))
384+
},
385+
_ => Some(format!("assert!({slice_str}.len() == {})", highest_index + 1)),
394386
},
395387
_ => None,
396388
};
@@ -405,7 +397,7 @@ fn report_indexes(cx: &LateContext<'_>, map: UnindexMap<u64, Vec<IndexEntry<'_>>
405397
assert_span,
406398
"provide the highest index that is indexed with",
407399
sugg,
408-
Applicability::MachineApplicable,
400+
app,
409401
);
410402
},
411403
);

0 commit comments

Comments
 (0)