Skip to content

Commit 3355f4b

Browse files
committed
Suggest constant on unused binding in a pattern
``` error: unused variable: `Batery` --> $DIR/binding-typo-2.rs:110:9 | LL | Batery => {} | ^^^^^^ | help: if this is intentional, prefix it with an underscore | LL | _Batery => {} | + help: you might have meant to pattern match on the similarly named constant `Battery` | LL | Battery => {} | + ```
1 parent 00f6fe1 commit 3355f4b

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
lines changed

compiler/rustc_passes/src/liveness.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,8 +1716,25 @@ impl<'tcx> Liveness<'_, 'tcx> {
17161716
}
17171717
}
17181718
}
1719-
// FIXME(estebank): look for consts of the same type with similar names as well, not
1720-
// just unit structs and variants.
1719+
if typo.is_none() {
1720+
for (hir_id, _, span) in &hir_ids_and_spans {
1721+
let ty = self.typeck_results.node_type(*hir_id);
1722+
// Look for consts of the same type with similar names as well, not just unit
1723+
// structs and variants.
1724+
for def_id in self.ir.tcx.hir_body_owners() {
1725+
if let DefKind::Const = self.ir.tcx.def_kind(def_id)
1726+
&& self.ir.tcx.type_of(def_id).instantiate_identity() == ty
1727+
{
1728+
typo = Some(errors::PatternTypo {
1729+
span: *span,
1730+
code: with_no_trimmed_paths!(self.ir.tcx.def_path_str(def_id)),
1731+
kind: "constant".to_string(),
1732+
item_name: self.ir.tcx.item_name(def_id).to_string(),
1733+
});
1734+
}
1735+
}
1736+
}
1737+
}
17211738
if is_assigned {
17221739
self.ir.tcx.emit_node_span_lint(
17231740
lint::builtin::UNUSED_VARIABLES,

tests/ui/or-patterns/binding-typo-2.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ enum Lol {
55
Bar,
66
}
77
const Bat: () = ();
8+
const Battery: () = ();
89
struct Bay;
910

1011
fn foo(x: (Lol, Lol)) {
@@ -62,8 +63,8 @@ fn foo(x: (Lol, Lol)) {
6263
}
6364
fn bar(x: (Lol, Lol)) {
6465
use Lol::*;
65-
use Bat;
66-
use Bay;
66+
use ::Bat;
67+
use ::Bay;
6768
match &x {
6869
(Foo, _) | (Ban, Foo) => {}
6970
//~^ ERROR: variable `Ban` is not bound in all patterns
@@ -105,6 +106,12 @@ fn baz(x: (Lol, Lol)) {
105106
//~| HELP: if this is intentional, prefix it with an underscore
106107
//~| HELP: you might have meant to pattern match on the similarly named
107108
}
109+
match () {
110+
Batery => {}
111+
//~^ ERROR: unused variable: `Batery`
112+
//~| HELP: if this is intentional, prefix it with an underscore
113+
//~| HELP: you might have meant to pattern match on the similarly named constant
114+
}
108115
}
109116

110117
fn main() {

tests/ui/or-patterns/binding-typo-2.stderr

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0408]: variable `Ban` is not bound in all patterns
2-
--> $DIR/binding-typo-2.rs:13:9
2+
--> $DIR/binding-typo-2.rs:14:9
33
|
44
LL | (Foo, Bar) | (Ban, Foo) => {}
55
| ^^^^^^^^^^ --- variable not in all patterns
@@ -13,7 +13,7 @@ LL + (Foo, Bar) | (Bar, Foo) => {}
1313
|
1414

1515
error[E0408]: variable `Ban` is not bound in all patterns
16-
--> $DIR/binding-typo-2.rs:24:9
16+
--> $DIR/binding-typo-2.rs:25:9
1717
|
1818
LL | (Foo, _) | (Ban, Foo) => {}
1919
| ^^^^^^^^ --- variable not in all patterns
@@ -27,7 +27,7 @@ LL + (Foo, _) | (Bar, Foo) => {}
2727
|
2828

2929
error[E0408]: variable `Non` is not bound in all patterns
30-
--> $DIR/binding-typo-2.rs:43:15
30+
--> $DIR/binding-typo-2.rs:44:15
3131
|
3232
LL | Non | None => {}
3333
| --- ^^^^ pattern doesn't bind `Non`
@@ -40,7 +40,7 @@ LL | None | None => {}
4040
| +
4141

4242
error[E0408]: variable `Non` is not bound in all patterns
43-
--> $DIR/binding-typo-2.rs:53:15
43+
--> $DIR/binding-typo-2.rs:54:15
4444
|
4545
LL | Non | Some(_) => {}
4646
| --- ^^^^^^^ pattern doesn't bind `Non`
@@ -54,7 +54,7 @@ LL + core::option::Option::None | Some(_) => {}
5454
|
5555

5656
error[E0408]: variable `Ban` is not bound in all patterns
57-
--> $DIR/binding-typo-2.rs:68:9
57+
--> $DIR/binding-typo-2.rs:69:9
5858
|
5959
LL | (Foo, _) | (Ban, Foo) => {}
6060
| ^^^^^^^^ --- variable not in all patterns
@@ -78,7 +78,7 @@ LL + (Foo, _) | (Bat, Foo) => {}
7878
|
7979

8080
error[E0408]: variable `Ban` is not bound in all patterns
81-
--> $DIR/binding-typo-2.rs:85:9
81+
--> $DIR/binding-typo-2.rs:86:9
8282
|
8383
LL | (Foo, _) | (Ban, Foo) => {}
8484
| ^^^^^^^^ --- variable not in all patterns
@@ -97,7 +97,7 @@ LL + (Foo, _) | (Bat, Foo) => {}
9797
|
9898

9999
error: variable `Ban` is assigned to, but never used
100-
--> $DIR/binding-typo-2.rs:13:23
100+
--> $DIR/binding-typo-2.rs:14:23
101101
|
102102
LL | (Foo, Bar) | (Ban, Foo) => {}
103103
| ^^^
@@ -115,7 +115,7 @@ LL + (Foo, Bar) | (Lol::Bar, Foo) => {}
115115
|
116116

117117
error: variable `Ban` is assigned to, but never used
118-
--> $DIR/binding-typo-2.rs:24:21
118+
--> $DIR/binding-typo-2.rs:25:21
119119
|
120120
LL | (Foo, _) | (Ban, Foo) => {}
121121
| ^^^
@@ -128,7 +128,7 @@ LL + (Foo, _) | (Lol::Bar, Foo) => {}
128128
|
129129

130130
error: unused variable: `Non`
131-
--> $DIR/binding-typo-2.rs:36:9
131+
--> $DIR/binding-typo-2.rs:37:9
132132
|
133133
LL | Non => {}
134134
| ^^^
@@ -144,7 +144,7 @@ LL + std::prelude::v1::None => {}
144144
|
145145

146146
error: unused variable: `Non`
147-
--> $DIR/binding-typo-2.rs:43:9
147+
--> $DIR/binding-typo-2.rs:44:9
148148
|
149149
LL | Non | None => {}
150150
| ^^^
@@ -160,7 +160,7 @@ LL + std::prelude::v1::None | None => {}
160160
|
161161

162162
error: unused variable: `Non`
163-
--> $DIR/binding-typo-2.rs:53:9
163+
--> $DIR/binding-typo-2.rs:54:9
164164
|
165165
LL | Non | Some(_) => {}
166166
| ^^^
@@ -176,7 +176,7 @@ LL + std::prelude::v1::None | Some(_) => {}
176176
|
177177

178178
error: variable `Ban` is assigned to, but never used
179-
--> $DIR/binding-typo-2.rs:68:21
179+
--> $DIR/binding-typo-2.rs:69:21
180180
|
181181
LL | (Foo, _) | (Ban, Foo) => {}
182182
| ^^^
@@ -189,7 +189,7 @@ LL + (Foo, _) | (Lol::Bar, Foo) => {}
189189
|
190190

191191
error: variable `Ban` is assigned to, but never used
192-
--> $DIR/binding-typo-2.rs:85:21
192+
--> $DIR/binding-typo-2.rs:86:21
193193
|
194194
LL | (Foo, _) | (Ban, Foo) => {}
195195
| ^^^
@@ -202,7 +202,7 @@ LL + (Foo, _) | (Lol::Bar, Foo) => {}
202202
|
203203

204204
error: unused variable: `Ban`
205-
--> $DIR/binding-typo-2.rs:97:10
205+
--> $DIR/binding-typo-2.rs:98:10
206206
|
207207
LL | (Ban, _) => {}
208208
| ^^^
@@ -218,7 +218,7 @@ LL + (Lol::Bar, _) => {}
218218
|
219219

220220
error: unused variable: `Ban`
221-
--> $DIR/binding-typo-2.rs:103:9
221+
--> $DIR/binding-typo-2.rs:104:9
222222
|
223223
LL | Ban => {}
224224
| ^^^
@@ -233,6 +233,21 @@ LL - Ban => {}
233233
LL + Bay => {}
234234
|
235235

236-
error: aborting due to 15 previous errors
236+
error: unused variable: `Batery`
237+
--> $DIR/binding-typo-2.rs:110:9
238+
|
239+
LL | Batery => {}
240+
| ^^^^^^
241+
|
242+
help: if this is intentional, prefix it with an underscore
243+
|
244+
LL | _Batery => {}
245+
| +
246+
help: you might have meant to pattern match on the similarly named constant `Battery`
247+
|
248+
LL | Battery => {}
249+
| +
250+
251+
error: aborting due to 16 previous errors
237252

238253
For more information about this error, try `rustc --explain E0408`.

0 commit comments

Comments
 (0)