Skip to content

Commit 0e40f94

Browse files
committed
Auto merge of rust-lang#10346 - samueltardieu:issue-10331, r=Manishearth
Do not base map_entry lint suggestion on expanded code Fixes rust-lang#10331 changelog: [`map_entry`]: do not base suggestion on code expanded by the compiler
2 parents a182a67 + e4e5924 commit 0e40f94

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

clippy_lints/src/entry.rs

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ declare_lint_pass!(HashMapPass => [MAP_ENTRY]);
6565
impl<'tcx> LateLintPass<'tcx> for HashMapPass {
6666
#[expect(clippy::too_many_lines)]
6767
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
68+
if expr.span.from_expansion() {
69+
return;
70+
}
71+
6872
let Some(higher::If { cond: cond_expr, then: then_expr, r#else: else_expr }) = higher::If::hir(expr) else {
6973
return
7074
};

tests/ui/entry.fixed

+14
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,18 @@ fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, m2: &mut HashMa
152152
});
153153
}
154154

155+
// Issue 10331
156+
// do not suggest a bad expansion because the compiler unrolls the first
157+
// occurrence of the loop
158+
pub fn issue_10331() {
159+
let mut m = HashMap::new();
160+
let mut i = 0;
161+
let mut x = 0;
162+
while !m.contains_key(&x) {
163+
m.insert(x, i);
164+
i += 1;
165+
x += 1;
166+
}
167+
}
168+
155169
fn main() {}

tests/ui/entry.rs

+14
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,18 @@ fn hash_map<K: Eq + Hash + Copy, V: Copy>(m: &mut HashMap<K, V>, m2: &mut HashMa
156156
}
157157
}
158158

159+
// Issue 10331
160+
// do not suggest a bad expansion because the compiler unrolls the first
161+
// occurrence of the loop
162+
pub fn issue_10331() {
163+
let mut m = HashMap::new();
164+
let mut i = 0;
165+
let mut x = 0;
166+
while !m.contains_key(&x) {
167+
m.insert(x, i);
168+
i += 1;
169+
x += 1;
170+
}
171+
}
172+
159173
fn main() {}

0 commit comments

Comments
 (0)