Skip to content

Commit 5670bc6

Browse files
committed
Just check if it contains _ in for pat
1 parent 8931633 commit 5670bc6

File tree

1 file changed

+1
-86
lines changed

1 file changed

+1
-86
lines changed

clippy_lints/src/loops.rs

+1-86
Original file line numberDiff line numberDiff line change
@@ -1052,82 +1052,6 @@ fn detect_manual_memcpy<'tcx>(
10521052
}
10531053
}
10541054

1055-
// Scans for the usage of the for loop pattern
1056-
struct ForPatternVisitor<'a, 'tcx> {
1057-
found_pattern: bool,
1058-
// Pattern that we are searching for
1059-
for_pattern: &'a Pat<'tcx>,
1060-
cx: &'a LateContext<'tcx>,
1061-
}
1062-
1063-
impl<'a, 'tcx> Visitor<'tcx> for ForPatternVisitor<'a, 'tcx> {
1064-
type Map = Map<'tcx>;
1065-
1066-
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
1067-
// Recursively explore an expression until a ExprKind::Path is found
1068-
match &expr.kind {
1069-
ExprKind::Array(expr_list) | ExprKind::MethodCall(_, _, expr_list, _) | ExprKind::Tup(expr_list) => {
1070-
for expr in *expr_list {
1071-
self.visit_expr(expr)
1072-
}
1073-
},
1074-
ExprKind::Binary(_, lhs_expr, rhs_expr) => {
1075-
self.visit_expr(lhs_expr);
1076-
self.visit_expr(rhs_expr);
1077-
},
1078-
ExprKind::Box(expr)
1079-
| ExprKind::Unary(_, expr)
1080-
| ExprKind::Cast(expr, _)
1081-
| ExprKind::Type(expr, _)
1082-
| ExprKind::AddrOf(_, _, expr)
1083-
| ExprKind::Field(expr, _)
1084-
| ExprKind::Struct(_, _, Some(expr)) => self.visit_expr(expr),
1085-
_ => {
1086-
// Exploration cannot continue ... calculate the hir_id of the current
1087-
// expr assuming it is a Path
1088-
if let Some(hir_id) = var_def_id(self.cx, &expr) {
1089-
// Pattern is found
1090-
if hir_id == self.for_pattern.hir_id {
1091-
self.found_pattern = true;
1092-
}
1093-
// If the for loop pattern is a tuple, determine whether the current
1094-
// expr is inside that tuple pattern
1095-
if let PatKind::Tuple(pat_list, _) = &self.for_pattern.kind {
1096-
let hir_id_list: Vec<HirId> = pat_list.iter().map(|p| p.hir_id).collect();
1097-
if hir_id_list.contains(&hir_id) {
1098-
self.found_pattern = true;
1099-
}
1100-
}
1101-
}
1102-
},
1103-
}
1104-
}
1105-
1106-
// This is triggered by walk_expr() for the case of vec.push(pat)
1107-
fn visit_qpath(&mut self, qpath: &'tcx QPath<'_>, _: HirId, _: Span) {
1108-
if_chain! {
1109-
if let QPath::Resolved(_, path) = qpath;
1110-
if let Res::Local(hir_id) = &path.res;
1111-
then {
1112-
if *hir_id == self.for_pattern.hir_id{
1113-
self.found_pattern = true;
1114-
}
1115-
1116-
if let PatKind::Tuple(pat_list, _) = &self.for_pattern.kind {
1117-
let hir_id_list: Vec<HirId> = pat_list.iter().map(|p| p.hir_id).collect();
1118-
if hir_id_list.contains(&hir_id) {
1119-
self.found_pattern = true;
1120-
}
1121-
}
1122-
}
1123-
}
1124-
}
1125-
1126-
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
1127-
NestedVisitorMap::None
1128-
}
1129-
}
1130-
11311055
// Scans the body of the for loop and determines whether lint should be given
11321056
struct SameItemPushVisitor<'a, 'tcx> {
11331057
should_lint: bool,
@@ -1218,16 +1142,7 @@ fn detect_same_item_push<'tcx>(
12181142
if let Some((vec, pushed_item)) = same_item_push_visitor.vec_push {
12191143
// Make sure that the push does not involve possibly mutating values
12201144
if mutated_variables(pushed_item, cx).map_or(false, |mutvars| mutvars.is_empty()) {
1221-
// Walk through the expression being pushed and make sure that it
1222-
// does not contain the for loop pattern
1223-
let mut for_pat_visitor = ForPatternVisitor {
1224-
found_pattern: false,
1225-
for_pattern: pat,
1226-
cx,
1227-
};
1228-
walk_expr(&mut for_pat_visitor, pushed_item);
1229-
1230-
if !for_pat_visitor.found_pattern {
1145+
if let PatKind::Wild = pat.kind {
12311146
let vec_str = snippet_with_macro_callsite(cx, vec.span, "");
12321147
let item_str = snippet_with_macro_callsite(cx, pushed_item.span, "");
12331148

0 commit comments

Comments
 (0)