Skip to content

Commit 71450c7

Browse files
committed
generalize bindings_with_variant_name lint
1 parent 900811e commit 71450c7

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

src/librustc_mir_build/hair/pattern/check_match.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,13 @@ impl<'tcx> Visitor<'tcx> for MatchVisitor<'_, 'tcx> {
6767
hir::LocalSource::AwaitDesugar => ("`await` future binding", None),
6868
};
6969
self.check_irrefutable(&loc.pat, msg, sp);
70-
71-
// Check legality of move bindings and `@` patterns.
7270
self.check_patterns(false, &loc.pat);
7371
}
7472

75-
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
76-
intravisit::walk_body(self, body);
77-
78-
for param in body.params {
79-
self.check_irrefutable(&param.pat, "function argument", None);
80-
self.check_patterns(false, &param.pat);
81-
}
73+
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
74+
intravisit::walk_param(self, param);
75+
self.check_irrefutable(&param.pat, "function argument", None);
76+
self.check_patterns(false, &param.pat);
8277
}
8378
}
8479

@@ -123,6 +118,7 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
123118
if !self.tcx.features().bindings_after_at {
124119
check_legality_of_bindings_in_at_patterns(self, pat);
125120
}
121+
check_for_bindings_named_same_as_variants(self, pat);
126122
}
127123

128124
fn check_match(
@@ -132,11 +128,8 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
132128
source: hir::MatchSource,
133129
) {
134130
for arm in arms {
135-
// First, check legality of move bindings.
131+
// Check the arm for some things unrelated to exhaustiveness.
136132
self.check_patterns(arm.guard.is_some(), &arm.pat);
137-
138-
// Second, perform some lints.
139-
check_for_bindings_named_same_as_variants(self, &arm.pat);
140133
}
141134

142135
let module = self.tcx.hir().get_module_parent(scrut.hir_id);

src/test/ui/lint/lint-uppercase-variables.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ fn main() {
2525
//~^^^ WARN unused variable: `Foo`
2626
}
2727

28+
let Foo = foo::Foo::Foo;
29+
//~^ ERROR variable `Foo` should have a snake case name
30+
//~^^ WARN `Foo` is named the same as one of the variants of the type `foo::Foo`
31+
//~^^^ WARN unused variable: `Foo`
32+
33+
fn in_param(Foo: foo::Foo) {}
34+
//~^ ERROR variable `Foo` should have a snake case name
35+
//~^^ WARN `Foo` is named the same as one of the variants of the type `foo::Foo`
36+
//~^^^ WARN unused variable: `Foo`
37+
2838
test(1);
2939

3040
let _ = Something { X: 0 };

src/test/ui/lint/lint-uppercase-variables.stderr

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ LL | Foo => {}
66
|
77
= note: `#[warn(bindings_with_variant_name)]` on by default
88

9+
warning[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `foo::Foo`
10+
--> $DIR/lint-uppercase-variables.rs:28:9
11+
|
12+
LL | let Foo = foo::Foo::Foo;
13+
| ^^^ help: to match on the variant, qualify the path: `foo::Foo::Foo`
14+
15+
warning[E0170]: pattern binding `Foo` is named the same as one of the variants of the type `foo::Foo`
16+
--> $DIR/lint-uppercase-variables.rs:33:17
17+
|
18+
LL | fn in_param(Foo: foo::Foo) {}
19+
| ^^^ help: to match on the variant, qualify the path: `foo::Foo::Foo`
20+
921
warning: unused variable: `Foo`
1022
--> $DIR/lint-uppercase-variables.rs:22:9
1123
|
@@ -19,6 +31,18 @@ LL | #![warn(unused)]
1931
| ^^^^^^
2032
= note: `#[warn(unused_variables)]` implied by `#[warn(unused)]`
2133

34+
warning: unused variable: `Foo`
35+
--> $DIR/lint-uppercase-variables.rs:28:9
36+
|
37+
LL | let Foo = foo::Foo::Foo;
38+
| ^^^ help: consider prefixing with an underscore: `_Foo`
39+
40+
warning: unused variable: `Foo`
41+
--> $DIR/lint-uppercase-variables.rs:33:17
42+
|
43+
LL | fn in_param(Foo: foo::Foo) {}
44+
| ^^^ help: consider prefixing with an underscore: `_Foo`
45+
2246
error: structure field `X` should have a snake case name
2347
--> $DIR/lint-uppercase-variables.rs:10:5
2448
|
@@ -49,6 +73,18 @@ error: variable `Foo` should have a snake case name
4973
LL | Foo => {}
5074
| ^^^ help: convert the identifier to snake case (notice the capitalization): `foo`
5175

52-
error: aborting due to 4 previous errors
76+
error: variable `Foo` should have a snake case name
77+
--> $DIR/lint-uppercase-variables.rs:28:9
78+
|
79+
LL | let Foo = foo::Foo::Foo;
80+
| ^^^ help: convert the identifier to snake case (notice the capitalization): `foo`
81+
82+
error: variable `Foo` should have a snake case name
83+
--> $DIR/lint-uppercase-variables.rs:33:17
84+
|
85+
LL | fn in_param(Foo: foo::Foo) {}
86+
| ^^^ help: convert the identifier to snake case (notice the capitalization): `foo`
87+
88+
error: aborting due to 6 previous errors
5389

5490
For more information about this error, try `rustc --explain E0170`.

0 commit comments

Comments
 (0)