Skip to content

Commit

Permalink
Rollup merge of rust-lang#66660 - jumbatm:dont_warn_about_snake_case_…
Browse files Browse the repository at this point in the history
…in_patterns, r=centril

Don't warn about snake case for field puns.

Closes rust-lang#66362.
  • Loading branch information
tmandry authored Jan 18, 2020
2 parents 689fca0 + 25a8f94 commit 77383a4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/librustc_lint/nonstandard_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,20 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
}

fn check_pat(&mut self, cx: &LateContext<'_, '_>, p: &hir::Pat<'_>) {
if let &PatKind::Binding(_, _, ident, _) = &p.kind {
if let &PatKind::Binding(_, hid, ident, _) = &p.kind {
if let hir::Node::Pat(parent_pat) = cx.tcx.hir().get(cx.tcx.hir().get_parent_node(hid))
{
if let PatKind::Struct(_, field_pats, _) = &parent_pat.kind {
for field in field_pats.iter() {
if field.ident != ident {
// Only check if a new name has been introduced, to avoid warning
// on both the struct definition and this pattern.
self.check_snake_case(cx, "variable", &ident);
}
}
return;
}
}
self.check_snake_case(cx, "variable", &ident);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#![deny(non_snake_case)]
#![allow(unused_variables)]
#![allow(dead_code)]

enum Foo {
Bad {
lowerCamelCaseName: bool,
//~^ ERROR structure field `lowerCamelCaseName` should have a snake case name
},
Good {
snake_case_name: bool,
},
}

fn main() {
let b = Foo::Bad { lowerCamelCaseName: true };

match b {
Foo::Bad { lowerCamelCaseName } => {}
Foo::Good { snake_case_name: lowerCamelCaseBinding } => { }
//~^ ERROR variable `lowerCamelCaseBinding` should have a snake case name
}

if let Foo::Good { snake_case_name: anotherLowerCamelCaseBinding } = b { }
//~^ ERROR variable `anotherLowerCamelCaseBinding` should have a snake case name

if let Foo::Bad { lowerCamelCaseName: yetAnotherLowerCamelCaseBinding } = b { }
//~^ ERROR variable `yetAnotherLowerCamelCaseBinding` should have a snake case name
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
error: structure field `lowerCamelCaseName` should have a snake case name
--> $DIR/issue-66362-no-snake-case-warning-for-field-puns.rs:7:9
|
LL | lowerCamelCaseName: bool,
| ^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `lower_camel_case_name`
|
note: lint level defined here
--> $DIR/issue-66362-no-snake-case-warning-for-field-puns.rs:1:9
|
LL | #![deny(non_snake_case)]
| ^^^^^^^^^^^^^^

error: variable `lowerCamelCaseBinding` should have a snake case name
--> $DIR/issue-66362-no-snake-case-warning-for-field-puns.rs:20:38
|
LL | Foo::Good { snake_case_name: lowerCamelCaseBinding } => { }
| ^^^^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `lower_camel_case_binding`

error: variable `anotherLowerCamelCaseBinding` should have a snake case name
--> $DIR/issue-66362-no-snake-case-warning-for-field-puns.rs:24:41
|
LL | if let Foo::Good { snake_case_name: anotherLowerCamelCaseBinding } = b { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `another_lower_camel_case_binding`

error: variable `yetAnotherLowerCamelCaseBinding` should have a snake case name
--> $DIR/issue-66362-no-snake-case-warning-for-field-puns.rs:27:43
|
LL | if let Foo::Bad { lowerCamelCaseName: yetAnotherLowerCamelCaseBinding } = b { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: convert the identifier to snake case: `yet_another_lower_camel_case_binding`

error: aborting due to 4 previous errors

0 comments on commit 77383a4

Please sign in to comment.