Skip to content

Commit 25a8f94

Browse files
committedJan 16, 2020
Don't warn about snake case for field puns that don't introduce a new name.
1 parent 632387f commit 25a8f94

3 files changed

+75
-1
lines changed
 

‎src/librustc_lint/nonstandard_style.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
350350
}
351351

352352
fn check_pat(&mut self, cx: &LateContext<'_, '_>, p: &hir::Pat<'_>) {
353-
if let &PatKind::Binding(_, _, ident, _) = &p.kind {
353+
if let &PatKind::Binding(_, hid, ident, _) = &p.kind {
354+
if let hir::Node::Pat(parent_pat) = cx.tcx.hir().get(cx.tcx.hir().get_parent_node(hid))
355+
{
356+
if let PatKind::Struct(_, field_pats, _) = &parent_pat.kind {
357+
for field in field_pats.iter() {
358+
if field.ident != ident {
359+
// Only check if a new name has been introduced, to avoid warning
360+
// on both the struct definition and this pattern.
361+
self.check_snake_case(cx, "variable", &ident);
362+
}
363+
}
364+
return;
365+
}
366+
}
354367
self.check_snake_case(cx, "variable", &ident);
355368
}
356369
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![deny(non_snake_case)]
2+
#![allow(unused_variables)]
3+
#![allow(dead_code)]
4+
5+
enum Foo {
6+
Bad {
7+
lowerCamelCaseName: bool,
8+
//~^ ERROR structure field `lowerCamelCaseName` should have a snake case name
9+
},
10+
Good {
11+
snake_case_name: bool,
12+
},
13+
}
14+
15+
fn main() {
16+
let b = Foo::Bad { lowerCamelCaseName: true };
17+
18+
match b {
19+
Foo::Bad { lowerCamelCaseName } => {}
20+
Foo::Good { snake_case_name: lowerCamelCaseBinding } => { }
21+
//~^ ERROR variable `lowerCamelCaseBinding` should have a snake case name
22+
}
23+
24+
if let Foo::Good { snake_case_name: anotherLowerCamelCaseBinding } = b { }
25+
//~^ ERROR variable `anotherLowerCamelCaseBinding` should have a snake case name
26+
27+
if let Foo::Bad { lowerCamelCaseName: yetAnotherLowerCamelCaseBinding } = b { }
28+
//~^ ERROR variable `yetAnotherLowerCamelCaseBinding` should have a snake case name
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: structure field `lowerCamelCaseName` should have a snake case name
2+
--> $DIR/issue-66362-no-snake-case-warning-for-field-puns.rs:7:9
3+
|
4+
LL | lowerCamelCaseName: bool,
5+
| ^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `lower_camel_case_name`
6+
|
7+
note: lint level defined here
8+
--> $DIR/issue-66362-no-snake-case-warning-for-field-puns.rs:1:9
9+
|
10+
LL | #![deny(non_snake_case)]
11+
| ^^^^^^^^^^^^^^
12+
13+
error: variable `lowerCamelCaseBinding` should have a snake case name
14+
--> $DIR/issue-66362-no-snake-case-warning-for-field-puns.rs:20:38
15+
|
16+
LL | Foo::Good { snake_case_name: lowerCamelCaseBinding } => { }
17+
| ^^^^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `lower_camel_case_binding`
18+
19+
error: variable `anotherLowerCamelCaseBinding` should have a snake case name
20+
--> $DIR/issue-66362-no-snake-case-warning-for-field-puns.rs:24:41
21+
|
22+
LL | if let Foo::Good { snake_case_name: anotherLowerCamelCaseBinding } = b { }
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `another_lower_camel_case_binding`
24+
25+
error: variable `yetAnotherLowerCamelCaseBinding` should have a snake case name
26+
--> $DIR/issue-66362-no-snake-case-warning-for-field-puns.rs:27:43
27+
|
28+
LL | if let Foo::Bad { lowerCamelCaseName: yetAnotherLowerCamelCaseBinding } = b { }
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `yet_another_lower_camel_case_binding`
30+
31+
error: aborting due to 4 previous errors
32+

0 commit comments

Comments
 (0)
Please sign in to comment.