Skip to content

Commit eb24acf

Browse files
committed
Auto merge of rust-lang#8165 - ebobrow:shadow_reuse_fn, r=xFrednet
fix [`shadow_reuse`] false negative for if let bindings fixes rust-lang#8087 changelog: trigger [`shadow_reuse`] instead of [`shadow_unrelated`] on shadowed `if let` bindings
2 parents 8529b2a + 1b67aa7 commit eb24acf

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

clippy_lints/src/shadow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ fn is_self_shadow(cx: &LateContext<'_>, pat: &Pat<'_>, mut expr: &Expr<'_>, hir_
220220
}
221221
}
222222

223-
/// Finds the "init" expression for a pattern: `let <pat> = <init>;` or
223+
/// Finds the "init" expression for a pattern: `let <pat> = <init>;` (or `if let`) or
224224
/// `match <init> { .., <pat> => .., .. }`
225225
fn find_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<&'tcx Expr<'tcx>> {
226226
for (_, node) in cx.tcx.hir().parent_iter(hir_id) {
227227
let init = match node {
228228
Node::Arm(_) | Node::Pat(_) => continue,
229229
Node::Expr(expr) => match expr.kind {
230-
ExprKind::Match(e, _, _) => Some(e),
230+
ExprKind::Match(e, _, _) | ExprKind::Let(_, e, _) => Some(e),
231231
_ => None,
232232
},
233233
Node::Local(local) => local.init,

tests/ui/shadow.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ fn syntax() {
4747
let _ = |[x]: [u32; 1]| {
4848
let x = 1;
4949
};
50+
let y = Some(1);
51+
if let Some(y) = y {}
5052
}
5153

5254
fn negative() {

tests/ui/shadow.stderr

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,29 @@ note: previous binding is here
241241
LL | let _ = |[x]: [u32; 1]| {
242242
| ^
243243

244+
error: `y` is shadowed
245+
--> $DIR/shadow.rs:51:17
246+
|
247+
LL | if let Some(y) = y {}
248+
| ^
249+
|
250+
note: previous binding is here
251+
--> $DIR/shadow.rs:50:9
252+
|
253+
LL | let y = Some(1);
254+
| ^
255+
244256
error: `_b` shadows a previous, unrelated binding
245-
--> $DIR/shadow.rs:85:9
257+
--> $DIR/shadow.rs:87:9
246258
|
247259
LL | let _b = _a;
248260
| ^^
249261
|
250262
note: previous binding is here
251-
--> $DIR/shadow.rs:84:28
263+
--> $DIR/shadow.rs:86:28
252264
|
253265
LL | pub async fn foo2(_a: i32, _b: i64) {
254266
| ^^
255267

256-
error: aborting due to 21 previous errors
268+
error: aborting due to 22 previous errors
257269

0 commit comments

Comments
 (0)