Skip to content

Commit ace4d36

Browse files
authored
Rollup merge of rust-lang#89473 - FabianWolff:issue-89469, r=joshtriplett
Fix extra `non_snake_case` warning for shorthand field bindings Fixes rust-lang#89469. The problem is the innermost `if` condition here: https://github.com/rust-lang/rust/blob/d14731cb3ced8318d7fc83cbe838f0e7f2fb3b40/compiler/rustc_lint/src/nonstandard_style.rs#L435-L452 This code runs for every `PatKind::Binding`, so if a struct has multiple fields, say A and B, and both are bound in a pattern using shorthands, the call to `self.check_snake_case()` will indeed be skipped in the `check_pat()` call for `A`; but when `check_pat()` is called for `B`, the loop will still iterate over `A`, and `field.ident (= A) != ident (= B)` will be true. I have fixed this by only looking at non-shorthand bindings, and only the binding that `check_pat()` was actually called for.
2 parents c81173c + 9626f2b commit ace4d36

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

compiler/rustc_lint/src/nonstandard_style.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -437,12 +437,13 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
437437
if let hir::Node::Pat(parent_pat) = cx.tcx.hir().get(cx.tcx.hir().get_parent_node(hid))
438438
{
439439
if let PatKind::Struct(_, field_pats, _) = &parent_pat.kind {
440-
for field in field_pats.iter() {
441-
if field.ident != ident {
442-
// Only check if a new name has been introduced, to avoid warning
443-
// on both the struct definition and this pattern.
444-
self.check_snake_case(cx, "variable", &ident);
445-
}
440+
if field_pats
441+
.iter()
442+
.any(|field| !field.is_shorthand && field.pat.hir_id == p.hir_id)
443+
{
444+
// Only check if a new name has been introduced, to avoid warning
445+
// on both the struct definition and this pattern.
446+
self.check_snake_case(cx, "variable", &ident);
446447
}
447448
return;
448449
}

src/test/ui/lint/issue-89469.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Regression test for #89469, where an extra non_snake_case warning was
2+
// reported for a shorthand field binding.
3+
4+
// check-pass
5+
#![deny(non_snake_case)]
6+
7+
#[allow(non_snake_case)]
8+
struct Entry {
9+
A: u16,
10+
a: u16
11+
}
12+
13+
fn foo() -> Entry {todo!()}
14+
15+
pub fn f() {
16+
let Entry { A, a } = foo();
17+
let _ = (A, a);
18+
}
19+
20+
fn main() {}

0 commit comments

Comments
 (0)