-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix missing_asserts_for_indexing ignores match for length
#17400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
9e0ae51
1b37dc2
d5560c8
afc877e
cd5f376
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,12 @@ 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, snippet_with_applicability}; | ||
| use clippy_utils::visitors::for_each_expr_without_closures; | ||
| use clippy_utils::{eq_expr_value, hash_expr}; | ||
| use clippy_utils::{eq_expr_value, hash_expr, is_wild}; | ||
| use rustc_ast::{BinOpKind, LitKind, RangeLimits}; | ||
| use rustc_data_structures::packed::Pu128; | ||
| use rustc_data_structures::unhash::UnindexMap; | ||
| use rustc_errors::{Applicability, Diag}; | ||
| use rustc_hir::{Block, Body, Expr, ExprKind, UnOp}; | ||
| use rustc_hir::{Arm, Block, Body, Expr, ExprKind, MatchSource, Node, PatExprKind, PatKind, UnOp}; | ||
| use rustc_lint::{LateContext, LateLintPass}; | ||
| use rustc_session::declare_lint_pass; | ||
| use rustc_span::{Span, Spanned, Symbol, sym}; | ||
|
|
@@ -123,6 +123,101 @@ fn len_comparison<'hir>( | |
| } | ||
| } | ||
|
|
||
| /// Checks if the index expression is inside a `match` on the slice's length | ||
| /// whose arms make the indexing infallible. | ||
| /// This only applies when the non-wildcard arms are integer literals that | ||
| /// contiguously cover `0..=max` otherwise the wildcard | ||
| /// arm gives no lower bound on the length and the lint fires as usual. | ||
| fn is_index_covered_by_match(cx: &LateContext<'_>, index_expr: &Expr<'_>, slice: &Expr<'_>) -> bool { | ||
| let mut containing_arm = None; | ||
| for (_, node) in cx.tcx.hir_parent_iter(index_expr.hir_id) { | ||
| match node { | ||
| Node::Expr(expr) => { | ||
| if let ExprKind::Match(scrutinee, arms, MatchSource::Normal) = expr.kind | ||
| && is_slice_len_expr(cx, scrutinee, slice) | ||
| && match_arms_bound_len(cx, index_expr, arms, containing_arm) | ||
| { | ||
| return arms.iter().any(|arm| is_wild(arm.pat)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assumes that if we have a wildcard anywhere, then the entire match is safe, even if we are accessing indexes we can't safely assume. fn foo(supported: &[u8]) {
match supported.len() {
0 => {},
1 => {},
2 => println!("{} {}", supported[0], supported[2]),
_ => println!("{} {}", supported[0], supported[2]),
}
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed -- literal arms now require match supported.len() {
0 => {},
1 => {},
2 => println!("{} {}", supported[2], supported[3]), // both out of bounds for len == 2 → lints
_ => {},
}The example doesn't lint but for a pre-existing reason unrelated to the suppression: match supported.len() {
0 => {},
1 => {},
2 => println!("{} {}", supported[0], supported[2]), // [0] in bounds → suppressed; [2] NOT suppressed
_ => println!("{} {}", supported[0], supported[2]), // both in bounds (len >= 3) → suppressed
} |
||
| } | ||
| }, | ||
| Node::Block(_) | Node::Stmt(_) | Node::LetStmt(_) => {}, | ||
| Node::Arm(arm) => containing_arm = Some(arm), | ||
| _ => return false, | ||
| } | ||
| } | ||
| false | ||
| } | ||
|
|
||
| /// Checks if the non-wildcard arms are integer literals contiguously covering | ||
| /// `0..=max`, so that the wildcard arm implies `len > max`. Returns `true` | ||
| /// only if the index used is at most `max`, i.e. provably in bounds. | ||
| fn match_arms_bound_len( | ||
| cx: &LateContext<'_>, | ||
| index_expr: &Expr<'_>, | ||
| arms: &[Arm<'_>], | ||
| containing_arm: Option<&Arm<'_>>, | ||
| ) -> bool { | ||
| let ExprKind::Index(_, index_lit, _) = index_expr.kind else { | ||
| return false; | ||
| }; | ||
| let Some(index) = upper_index_expr(cx, index_lit) else { | ||
| return false; | ||
| }; | ||
|
|
||
| // index inside a literal arm `n => ...`: len is exactly `n` there, | ||
| // so anything at or past `n` is out of bounds | ||
| if let Some(arm) = containing_arm | ||
| && let Some(n) = arm_len_literal(arm) | ||
| && index >= n | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| let mut matched_lens = Vec::new(); | ||
| for arm in arms { | ||
| if arm.guard.is_some() { | ||
| return false; | ||
| } | ||
| if is_wild(arm.pat) { | ||
| continue; | ||
| } | ||
| let Some(n) = arm_len_literal(arm) else { | ||
| return false; | ||
| }; | ||
| matched_lens.push(n); | ||
| } | ||
|
|
||
| matched_lens.sort_unstable(); | ||
| matched_lens.dedup(); | ||
|
|
||
| // literals must be exactly 0..=max, so `_` implies len > max | ||
| let Some(&max) = matched_lens.last() else { | ||
| return false; | ||
| }; | ||
| matched_lens.len() == max + 1 && index <= max | ||
| } | ||
|
|
||
| fn arm_len_literal(arm: &Arm<'_>) -> Option<usize> { | ||
| if let PatKind::Expr(pat_expr) = arm.pat.kind | ||
| && let PatExprKind::Lit { lit, .. } = pat_expr.kind | ||
| && let LitKind::Int(Pu128(n), _) = lit.node | ||
| { | ||
| Some(n as usize) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| fn is_slice_len_expr(cx: &LateContext<'_>, expr: &Expr<'_>, slice: &Expr<'_>) -> bool { | ||
| if let ExprKind::MethodCall(method, recv, [], _) = expr.kind | ||
| && method.ident.name == sym::len | ||
| { | ||
| eq_expr_value(cx, expr.span.ctxt(), recv, slice) | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| /// Attempts to extract parts out of an `assert!`-like expression | ||
| /// in the form `assert!(some_slice.len() > 5)`. | ||
| /// | ||
|
|
@@ -239,6 +334,7 @@ fn check_index<'hir>(cx: &LateContext<'_>, expr: &'hir Expr<'hir>, map: &mut Uni | |
| if let ExprKind::Index(slice, index_lit, _) = expr.kind | ||
| && cx.typeck_results().expr_ty_adjusted(slice).peel_refs().is_slice() | ||
| && let Some(index) = upper_index_expr(cx, index_lit) | ||
| && !is_index_covered_by_match(cx, expr, slice) | ||
| { | ||
| let hash = hash_expr(cx, slice); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.