Skip to content

Commit 7211ea0

Browse files
Fixed problem with tuple patterns
1 parent 4f9d633 commit 7211ea0

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

clippy_lints/src/loops.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ fn has_mutable_variables<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr)
11101110
struct ForPatternVisitor<'a, 'tcx> {
11111111
found_pattern: bool,
11121112
// Pattern that we are searching for
1113-
for_pattern_hir_id: HirId,
1113+
for_pattern: &'a rustc::hir::Pat,
11141114
cx: &'a LateContext<'a, 'tcx>,
11151115
}
11161116

@@ -1138,9 +1138,17 @@ impl<'a, 'tcx> Visitor<'tcx> for ForPatternVisitor<'a, 'tcx> {
11381138
// expr assuming it is a Path
11391139
if let Some(hir_id) = var_def_id(self.cx, &expr) {
11401140
// Pattern is found
1141-
if hir_id == self.for_pattern_hir_id {
1141+
if hir_id == self.for_pattern.hir_id {
11421142
self.found_pattern = true;
11431143
}
1144+
// If the for loop pattern is a tuple, determine whether the current
1145+
// expr is inside that tuple pattern
1146+
if let PatKind::Tuple(pat_list, _) = &self.for_pattern.kind {
1147+
let hir_id_list: Vec<HirId> = pat_list.iter().map(|p| p.hir_id).collect();
1148+
if hir_id_list.contains(&hir_id) {
1149+
self.found_pattern = true;
1150+
}
1151+
}
11441152
}
11451153
},
11461154
}
@@ -1151,9 +1159,17 @@ impl<'a, 'tcx> Visitor<'tcx> for ForPatternVisitor<'a, 'tcx> {
11511159
if_chain! {
11521160
if let rustc::hir::QPath::Resolved(_, path) = qpath;
11531161
if let rustc::hir::def::Res::Local(hir_id) = &path.res;
1154-
if *hir_id == self.for_pattern_hir_id;
11551162
then {
1156-
self.found_pattern = true;
1163+
if *hir_id == self.for_pattern.hir_id{
1164+
self.found_pattern = true;
1165+
}
1166+
1167+
if let PatKind::Tuple(pat_list, _) = &self.for_pattern.kind {
1168+
let hir_id_list: Vec<HirId> = pat_list.iter().map(|p| p.hir_id).collect();
1169+
if hir_id_list.contains(&hir_id) {
1170+
self.found_pattern = true;
1171+
}
1172+
}
11571173
}
11581174
}
11591175
}
@@ -1255,7 +1271,7 @@ fn detect_same_item_push<'a, 'tcx>(
12551271
// does not contain the for loop pattern
12561272
let mut for_pat_visitor = ForPatternVisitor {
12571273
found_pattern: false,
1258-
for_pattern_hir_id: pat.hir_id,
1274+
for_pattern: pat,
12591275
cx,
12601276
};
12611277
intravisit::walk_expr(&mut for_pat_visitor, pushed_item);

tests/ui/same_item_push.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ fn main() {
4747
vec6.push(mutate_increment(item2));
4848
}
4949

50-
let mut vec7: Vec<u8> = Vec::new();
51-
for i in 0..30 {
52-
vec7.push(i);
50+
let mut vec7: Vec<usize> = Vec::new();
51+
for (a, b) in [0, 1, 4, 9, 16].iter().enumerate() {
52+
vec7.push(a);
5353
}
5454

5555
let mut vec8: Vec<u8> = Vec::new();

0 commit comments

Comments
 (0)