Skip to content

Incorrect non_shorthand_field_patterns warning #65924

Closed
@kuviman

Description

@kuviman
struct Foo {
    int: i32,
    text: String,
}

fn foo(foo: &Foo) {
    match *foo {
        Foo { int, text: ref text } => {
            println!("{:?}, {:?}", int, text);
        }
    }
}

This code gives incorrect warning:

warning: the `text:` in this pattern is redundant
 --> src/lib.rs:8:24
  |
8 |             Foo { int, text: ref text } => {
  |                        -----^^^^^^^^^
  |                        |
  |                        help: remove this
  |
  = note: `#[warn(non_shorthand_field_patterns)]` on by default

The suggested fix makes text move, which is impossible since foo is borrowed.

Using auto borrowing with match foo instead of match *foo would make all fields borrowed, but I want most of them moved (copied).

Link to playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions