Skip to content

Incorrect non_shorthand_field_patterns warning #65924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kuviman opened this issue Oct 29, 2019 · 1 comment
Closed

Incorrect non_shorthand_field_patterns warning #65924

kuviman opened this issue Oct 29, 2019 · 1 comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@kuviman
Copy link

kuviman commented Oct 29, 2019

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

@jonas-schievink jonas-schievink added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 29, 2019
@csmoe
Copy link
Member

csmoe commented Nov 15, 2019

Given warning: the text: in this pattern is redundant, it's suggesting to remove text:, not ref text as:

Foo { int, ref text }

@csmoe csmoe closed this as completed Nov 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants