Skip to content

Commit d005b39

Browse files
committed
Fix missing_asserts_for_indexing logic and test
1 parent 540f37b commit d005b39

File tree

3 files changed

+50
-385
lines changed

3 files changed

+50
-385
lines changed

src/tools/clippy/clippy_lints/src/missing_asserts_for_indexing.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_ast::{LitKind, RangeLimits};
1010
use rustc_data_structures::packed::Pu128;
1111
use rustc_data_structures::unhash::UnindexMap;
1212
use rustc_errors::{Applicability, Diag};
13-
use rustc_hir::{BinOp, Block, Body, Expr, ExprKind, UnOp, StmtKind};
13+
use rustc_hir::{BinOp, Body, Expr, ExprKind};
1414
use rustc_lint::{LateContext, LateLintPass};
1515
use rustc_session::declare_lint_pass;
1616
use rustc_span::source_map::Spanned;
@@ -134,22 +134,16 @@ fn assert_len_expr<'hir>(
134134
cx: &LateContext<'_>,
135135
expr: &'hir Expr<'hir>,
136136
) -> Option<(LengthComparison, usize, &'hir Expr<'hir>)> {
137-
if let Some(higher::If { cond, then, .. }) = higher::If::hir(expr)
138-
&& let ExprKind::Unary(UnOp::Not, condition) = &cond.kind
139-
&& let ExprKind::Block(block, None) = condition.kind
140-
&& let [local] = block.stmts
141-
&& let StmtKind::Let(local) = local.kind
142-
&& let Some(condition) = local.init
143-
&& let ExprKind::Binary(bin_op, left, right) = &condition.kind
137+
if let higher::IfLetOrMatch::Match(cond, [_, then], _) = higher::IfLetOrMatch::parse(cx, expr)?
138+
&& let ExprKind::Binary(bin_op, left, right) = &cond.kind
144139

145140
&& let Some((cmp, asserted_len, slice_len)) = len_comparison(*bin_op, left, right)
146141
&& let ExprKind::MethodCall(method, recv, [], _) = &slice_len.kind
147142
&& cx.typeck_results().expr_ty_adjusted(recv).peel_refs().is_slice()
148143
&& method.ident.name == sym::len
149144

150145
// check if `then` block has a never type expression
151-
&& let ExprKind::Block(Block { expr: Some(then_expr), .. }, _) = then.kind
152-
&& cx.typeck_results().expr_ty(then_expr).is_never()
146+
&& cx.typeck_results().expr_ty(then.body).is_never()
153147
{
154148
Some((cmp, asserted_len, recv))
155149
} else {
@@ -341,21 +335,21 @@ fn report_indexes(cx: &LateContext<'_>, map: &UnindexMap<u64, Vec<IndexEntry<'_>
341335
// `v.len() < 5` and `v.len() <= 5` does nothing in terms of bounds checks.
342336
// The user probably meant `v.len() > 5`
343337
LengthComparison::LengthLessThanInt | LengthComparison::LengthLessThanOrEqualInt => Some(
344-
format!("assert!({}.len() > {highest_index})", snippet(cx, slice.span, "..")),
338+
format!("{}.len() > {highest_index}", snippet(cx, slice.span, "..")),
345339
),
346340
// `5 < v.len()` == `v.len() > 5`
347341
LengthComparison::IntLessThanLength if asserted_len < highest_index => Some(format!(
348-
"assert!({}.len() > {highest_index})",
342+
"{}.len() > {highest_index}",
349343
snippet(cx, slice.span, "..")
350344
)),
351345
// `5 <= v.len() == `v.len() >= 5`
352346
LengthComparison::IntLessThanOrEqualLength if asserted_len <= highest_index => Some(format!(
353-
"assert!({}.len() > {highest_index})",
347+
"{}.len() > {highest_index}",
354348
snippet(cx, slice.span, "..")
355349
)),
356350
// `highest_index` here is rather a length, so we need to add 1 to it
357351
LengthComparison::LengthEqualInt if asserted_len < highest_index + 1 => Some(format!(
358-
"assert!({}.len() == {})",
352+
"{}.len() == {}",
359353
snippet(cx, slice.span, ".."),
360354
highest_index + 1
361355
)),

0 commit comments

Comments
 (0)