Skip to content
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

field_reassign_with_default should not expand macros in suggestion #6522

Closed
matthiaskrgr opened this issue Dec 30, 2020 · 0 comments · Fixed by #6531
Closed

field_reassign_with_default should not expand macros in suggestion #6522

matthiaskrgr opened this issue Dec 30, 2020 · 0 comments · Fixed by #6531
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@matthiaskrgr
Copy link
Member

Code like this (notice the vec![] macro)

#[derive(Default)]
struct S {
    n: i32,
    v: Vec<String>,
    s: String,
}

pub fn main() {
    let mut s = S::default();
    s.s = String::from("hello world");
    s.v = vec![String::from("a"), 'b'.into()];
}

gets expanded in the suggestion, this should not happen I believe.

    Checking x v0.1.0 (/tmp/x)
warning: field is never read: `n`
 --> src/main.rs:3:5
  |
3 |     n: i32,
  |     ^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: field assignment outside of initializer for an instance created with Default::default()
  --> src/main.rs:10:5
   |
10 |     s.s = String::from("hello world");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(clippy::field_reassign_with_default)]` on by default
note: consider initializing the variable with `S { s: String::from("hello world"), v: <[_]>::into_vec(box [$($x),+]), ..Default::default() }` and removing relevant reassignments
  --> src/main.rs:9:5
   |
9  |     let mut s = S::default();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#field_reassign_with_default

warning: 2 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.09s
 

clippy 0.0.212 (0d8a27a6f 2020-12-28)

@matthiaskrgr matthiaskrgr added the C-bug Category: Clippy is not doing the correct thing label Dec 30, 2020
bors added a commit that referenced this issue Jan 2, 2021
field_reassign_with_default: don't expand macros in suggestion

fixes #6522

changelog: field_reassign_with_default: don't expand macros in lint suggestion (#6522)
@bors bors closed this as completed in 5d48b91 Jan 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant