Skip to content

Commit 9fe2500

Browse files
committed
Add more detail to irrefutable let pattern lint
1 parent 5b7c07d commit 9fe2500

File tree

6 files changed

+60
-53
lines changed

6 files changed

+60
-53
lines changed

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -366,31 +366,38 @@ fn unreachable_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, catchall: Option<
366366
}
367367

368368
fn irrefutable_let_pattern(tcx: TyCtxt<'_>, span: Span, id: HirId, source: hir::MatchSource) {
369-
tcx.struct_span_lint_hir(IRREFUTABLE_LET_PATTERNS, id, span, |lint| match source {
370-
hir::MatchSource::IfLetDesugar { .. } => {
371-
let mut diag = lint.build("irrefutable `if let` pattern");
372-
diag.note("this pattern will always match, so the `if let` is useless");
373-
diag.help("consider replacing the `if let` with a `let`");
374-
diag.emit()
375-
}
376-
hir::MatchSource::WhileLetDesugar { .. } => {
377-
let mut diag = lint.build("irrefutable `while let` pattern");
378-
diag.note("this pattern will always match, so the loop will never exit");
379-
diag.help("consider instead using a `loop { ... }` with a `let` inside it");
380-
diag.emit()
381-
}
382-
hir::MatchSource::IfLetGuardDesugar { .. }=> {
383-
let mut diag = lint.build("irrefutable `if let` guard pattern");
384-
diag.note("this pattern will always match, so the guard is useless");
385-
diag.help("consider removing the guard and adding a `let` inside the match arm");
386-
diag.emit()
387-
}
388-
_ => {
389-
bug!(
390-
"expected `if let`, `while let`, or `if let` guard HIR match source, found {:?}",
391-
source,
392-
)
393-
}
369+
tcx.struct_span_lint_hir(IRREFUTABLE_LET_PATTERNS, id, span, |lint| {
370+
let mut diag = match source {
371+
hir::MatchSource::IfLetDesugar { .. } => {
372+
let mut diag = lint.build("irrefutable `if let` pattern");
373+
diag.span_label(span, "this pattern will always match, so the `if let` is useless");
374+
diag.help("consider replacing the `if let` with a `let`");
375+
diag
376+
}
377+
hir::MatchSource::WhileLetDesugar { .. } => {
378+
let mut diag = lint.build("irrefutable `while let` pattern");
379+
diag.span_label(span, "this pattern will always match, so the loop will never exit");
380+
diag.help("consider instead using a `loop { ... }` with a `let` inside it");
381+
diag
382+
}
383+
hir::MatchSource::IfLetGuardDesugar { .. }=> {
384+
let mut diag = lint.build("irrefutable `if let` guard pattern");
385+
diag.span_label(span, "this pattern will always match, so the guard is useless");
386+
diag.help("consider removing the guard and adding a `let` inside the match arm");
387+
diag
388+
}
389+
_ => {
390+
bug!(
391+
"expected `if let`, `while let`, or `if let` guard HIR match source, found {:?}",
392+
source,
393+
)
394+
}
395+
};
396+
diag.help(
397+
"for more information, visit \
398+
<https://doc.rust-lang.org/book/ch18-02-refutability.html>",
399+
);
400+
diag.emit();
394401
});
395402
}
396403

src/test/ui/closures/2229_closure_analysis/diagnostics/closure-origin-single-variant-diagnostics.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ warning: irrefutable `if let` pattern
1111
--> $DIR/closure-origin-single-variant-diagnostics.rs:18:12
1212
|
1313
LL | if let SingleVariant::Point(ref mut x, _) = point {
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this pattern will always match, so the `if let` is useless
1515
|
1616
= note: `#[warn(irrefutable_let_patterns)]` on by default
17-
= note: this pattern will always match, so the `if let` is useless
1817
= help: consider replacing the `if let` with a `let`
18+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
1919

2020
error[E0382]: use of moved value: `c`
2121
--> $DIR/closure-origin-single-variant-diagnostics.rs:25:13

src/test/ui/expr/if/if-let.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,68 @@ warning: irrefutable `if let` pattern
22
--> $DIR/if-let.rs:6:16
33
|
44
LL | if let $p = $e $b
5-
| ^^^
5+
| ^^^ this pattern will always match, so the `if let` is useless
66
...
77
LL | / foo!(a, 1, {
88
LL | | println!("irrefutable pattern");
99
LL | | });
1010
| |_______- in this macro invocation
1111
|
1212
= note: `#[warn(irrefutable_let_patterns)]` on by default
13-
= note: this pattern will always match, so the `if let` is useless
1413
= help: consider replacing the `if let` with a `let`
14+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
1515
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1616

1717
warning: irrefutable `if let` pattern
1818
--> $DIR/if-let.rs:6:16
1919
|
2020
LL | if let $p = $e $b
21-
| ^^^
21+
| ^^^ this pattern will always match, so the `if let` is useless
2222
...
2323
LL | / bar!(a, 1, {
2424
LL | | println!("irrefutable pattern");
2525
LL | | });
2626
| |_______- in this macro invocation
2727
|
28-
= note: this pattern will always match, so the `if let` is useless
2928
= help: consider replacing the `if let` with a `let`
29+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
3030
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
3131

3232
warning: irrefutable `if let` pattern
3333
--> $DIR/if-let.rs:26:8
3434
|
3535
LL | if let a = 1 {
36-
| ^^^^^^^^^
36+
| ^^^^^^^^^ this pattern will always match, so the `if let` is useless
3737
|
38-
= note: this pattern will always match, so the `if let` is useless
3938
= help: consider replacing the `if let` with a `let`
39+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
4040

4141
warning: irrefutable `if let` pattern
4242
--> $DIR/if-let.rs:30:8
4343
|
4444
LL | if let a = 1 {
45-
| ^^^^^^^^^
45+
| ^^^^^^^^^ this pattern will always match, so the `if let` is useless
4646
|
47-
= note: this pattern will always match, so the `if let` is useless
4847
= help: consider replacing the `if let` with a `let`
48+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
4949

5050
warning: irrefutable `if let` pattern
5151
--> $DIR/if-let.rs:40:15
5252
|
5353
LL | } else if let a = 1 {
54-
| ^^^^^^^^^
54+
| ^^^^^^^^^ this pattern will always match, so the `if let` is useless
5555
|
56-
= note: this pattern will always match, so the `if let` is useless
5756
= help: consider replacing the `if let` with a `let`
57+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
5858

5959
warning: irrefutable `if let` pattern
6060
--> $DIR/if-let.rs:46:15
6161
|
6262
LL | } else if let a = 1 {
63-
| ^^^^^^^^^
63+
| ^^^^^^^^^ this pattern will always match, so the `if let` is useless
6464
|
65-
= note: this pattern will always match, so the `if let` is useless
6665
= help: consider replacing the `if let` with a `let`
66+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
6767

6868
warning: 6 warnings emitted
6969

src/test/ui/pattern/usefulness/deny-irrefutable-let-patterns.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@ error: irrefutable `if let` pattern
22
--> $DIR/deny-irrefutable-let-patterns.rs:7:8
33
|
44
LL | if let _ = 5 {}
5-
| ^^^^^^^^^
5+
| ^^^^^^^^^ this pattern will always match, so the `if let` is useless
66
|
77
note: the lint level is defined here
88
--> $DIR/deny-irrefutable-let-patterns.rs:4:9
99
|
1010
LL | #![deny(irrefutable_let_patterns)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^
12-
= note: this pattern will always match, so the `if let` is useless
1312
= help: consider replacing the `if let` with a `let`
13+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
1414

1515
error: irrefutable `while let` pattern
1616
--> $DIR/deny-irrefutable-let-patterns.rs:9:11
1717
|
1818
LL | while let _ = 5 {
19-
| ^^^^^^^^^
19+
| ^^^^^^^^^ this pattern will always match, so the loop will never exit
2020
|
21-
= note: this pattern will always match, so the loop will never exit
2221
= help: consider instead using a `loop { ... }` with a `let` inside it
22+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
2323

2424
error: irrefutable `if let` guard pattern
2525
--> $DIR/deny-irrefutable-let-patterns.rs:14:14
2626
|
2727
LL | _ if let _ = 2 => {}
28-
| ^^^^^^^^^
28+
| ^^^^^^^^^ this pattern will always match, so the guard is useless
2929
|
30-
= note: this pattern will always match, so the guard is useless
3130
= help: consider removing the guard and adding a `let` inside the match arm
31+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
3232

3333
error: aborting due to 3 previous errors
3434

src/test/ui/rfc-2294-if-let-guard/warns.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ error: irrefutable `if let` guard pattern
22
--> $DIR/warns.rs:7:20
33
|
44
LL | Some(x) if let () = x => {}
5-
| ^^^^^^^^^^
5+
| ^^^^^^^^^^ this pattern will always match, so the guard is useless
66
|
77
note: the lint level is defined here
88
--> $DIR/warns.rs:4:8
99
|
1010
LL | #[deny(irrefutable_let_patterns)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^
12-
= note: this pattern will always match, so the guard is useless
1312
= help: consider removing the guard and adding a `let` inside the match arm
13+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
1414

1515
error: unreachable pattern
1616
--> $DIR/warns.rs:16:25

src/test/ui/while-let.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,41 @@ warning: irrefutable `while let` pattern
22
--> $DIR/while-let.rs:7:19
33
|
44
LL | while let $p = $e $b
5-
| ^^^
5+
| ^^^ this pattern will always match, so the loop will never exit
66
...
77
LL | / foo!(_a, 1, {
88
LL | | println!("irrefutable pattern");
99
LL | | });
1010
| |_______- in this macro invocation
1111
|
1212
= note: `#[warn(irrefutable_let_patterns)]` on by default
13-
= note: this pattern will always match, so the loop will never exit
1413
= help: consider instead using a `loop { ... }` with a `let` inside it
14+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
1515
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
1616

1717
warning: irrefutable `while let` pattern
1818
--> $DIR/while-let.rs:7:19
1919
|
2020
LL | while let $p = $e $b
21-
| ^^^
21+
| ^^^ this pattern will always match, so the loop will never exit
2222
...
2323
LL | / bar!(_a, 1, {
2424
LL | | println!("irrefutable pattern");
2525
LL | | });
2626
| |_______- in this macro invocation
2727
|
28-
= note: this pattern will always match, so the loop will never exit
2928
= help: consider instead using a `loop { ... }` with a `let` inside it
29+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
3030
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
3131

3232
warning: irrefutable `while let` pattern
3333
--> $DIR/while-let.rs:27:11
3434
|
3535
LL | while let _a = 1 {
36-
| ^^^^^^^^^^
36+
| ^^^^^^^^^^ this pattern will always match, so the loop will never exit
3737
|
38-
= note: this pattern will always match, so the loop will never exit
3938
= help: consider instead using a `loop { ... }` with a `let` inside it
39+
= help: for more information, visit <https://doc.rust-lang.org/book/ch18-02-refutability.html>
4040

4141
warning: 3 warnings emitted
4242

0 commit comments

Comments
 (0)