Skip to content

Fix needless_range_loop FP and FN on multidimensional array #15209

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions clippy_lints/src/loops/needless_range_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::{Visitor, walk_expr};
use rustc_hir::{BinOpKind, BorrowKind, Closure, Expr, ExprKind, HirId, Mutability, Pat, PatKind, QPath};
use rustc_hir::{BinOpKind, BorrowKind, Closure, Expr, ExprKind, HirId, Mutability, Node, Pat, PatKind, QPath};
use rustc_lint::LateContext;
use rustc_middle::middle::region;
use rustc_middle::ty::{self, Ty};
Expand Down Expand Up @@ -241,12 +241,18 @@ struct VarVisitor<'a, 'tcx> {
}

impl<'tcx> VarVisitor<'_, 'tcx> {
fn check(&mut self, idx: &'tcx Expr<'_>, seqexpr: &'tcx Expr<'_>, expr: &'tcx Expr<'_>) -> bool {
fn check<'a>(&mut self, idxs: &mut Vec<&'tcx Expr<'a>>, seqexpr: &'tcx Expr<'a>, expr: &'tcx Expr<'_>) -> bool {
// Handle multi-dimensional array
if let ExprKind::Index(seqexpr, idx, _) = seqexpr.kind {
idxs.push(idx);
// Recursive call
return self.check(idxs, seqexpr, expr);
}
if let ExprKind::Path(ref seqpath) = seqexpr.kind
// the indexed container is referenced by a name
&& let QPath::Resolved(None, seqvar) = *seqpath
&& seqvar.segments.len() == 1
&& is_local_used(self.cx, idx, self.var)
&& let Some(idx) = idxs.iter().find(|idx| is_local_used(self.cx, **idx, self.var))
{
if self.prefer_mutable {
self.indexed_mut.insert(seqvar.segments[0].ident.name);
Expand Down Expand Up @@ -302,16 +308,27 @@ impl<'tcx> Visitor<'tcx> for VarVisitor<'_, 'tcx> {
.and_then(|def_id| self.cx.tcx.trait_of_item(def_id))
&& ((meth.ident.name == sym::index && self.cx.tcx.lang_items().index_trait() == Some(trait_id))
|| (meth.ident.name == sym::index_mut && self.cx.tcx.lang_items().index_mut_trait() == Some(trait_id)))
&& !self.check(args_1, args_0, expr)
{
return;
let mut idxs: Vec<&Expr<'_>> = vec![args_1];
if !self.check(&mut idxs, args_0, expr) {
return;
}
}

if let ExprKind::Index(seqexpr, idx, _) = expr.kind
if let ExprKind::Index(seqexpr, idx, _) = expr.kind {
// Only handle the top `Index` expr
if let Node::Expr(Expr {
kind: ExprKind::Index(_, _, _),
..
}) = self.cx.tcx.parent_hir_node(expr.hir_id)
{
return;
}
let mut idxs: Vec<&Expr<'_>> = vec![idx];
// an index op
&& !self.check(idx, seqexpr, expr)
{
return;
if !self.check(&mut idxs, seqexpr, expr) {
return;
}
}

if let ExprKind::Path(QPath::Resolved(None, path)) = expr.kind
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/needless_range_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,23 @@ mod issue_2496 {
unimplemented!()
}
}

fn issue_15068() {
let a = vec![vec![0u8; MAX_LEN]; MAX_LEN];
let b = vec![0u8; MAX_LEN];

for i in 0..MAX_LEN {
// no error
let _ = a[0][i];
let _ = b[i];
}

// Could be rewrited to:
// ```no_run
// for <item> in a[0].take(MAX_LEN) {
// ```
for i in 0..MAX_LEN {
//~^ needless_range_loop
let _ = a[0][i];
}
}
14 changes: 13 additions & 1 deletion tests/ui/needless_range_loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,17 @@ LL - for i in 0..vec.len() {
LL + for (i, <item>) in vec.iter_mut().enumerate() {
|

error: aborting due to 14 previous errors
error: the loop variable `i` is only used to index `a`
--> tests/ui/needless_range_loop.rs:203:14
|
LL | for i in 0..MAX_LEN {
| ^^^^^^^^^^
|
help: consider using an iterator
|
LL - for i in 0..MAX_LEN {
LL + for <item> in a.iter().take(MAX_LEN) {
|

error: aborting due to 15 previous errors