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

suggest struct constructor instead of mutable object + field mod #568

Closed
oli-obk opened this issue Jan 20, 2016 · 0 comments · Fixed by #5911
Closed

suggest struct constructor instead of mutable object + field mod #568

oli-obk opened this issue Jan 20, 2016 · 0 comments · Fixed by #5911
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-AST Type: Requires working with the AST

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Jan 20, 2016

If you have a struct where all fields are public, then you should never do let mut a = A::default(); a.x = 42; but instead use a constructor with .. A::default() at the end:

#[derive(Default)]
struct A {
    i: i32,
    j: i64,
}

fn main() {
    let mut a: A = Default::default();
    a.i = 42;
}

fn should_be() {
    let a = A {
        i: 42,
        .. Default::default()
    };
}
@Manishearth Manishearth added good-first-issue These issues are a good way to get started with Clippy T-AST Type: Requires working with the AST A-lint Area: New lints labels Jan 20, 2016
hegza pushed a commit to hegza/rust-clippy that referenced this issue Oct 31, 2020
- Implement `field_reassign_with_default` as a `LateLintPass`
- Avoid triggering `default_trait_access` on a span already linted by
`field_reassigned_with_default`
- Co-authored-by: Eduardo Broto <[email protected]>
- Fixes rust-lang#568
bors added a commit that referenced this issue Nov 4, 2020
Add lint for 'field_reassign_with_default` #568

changelog: Add lint for field_reassign_with_default that checks if mutable object + field modification is used to edit a binding initialized with Default::default() instead of struct constructor.

Fixes #568

Notes:
- Checks for reassignment of one or more fields of a binding initialized with Default::default().
- Implemented using EarlyLintPass, might be future proofed better with LateLintPass.
- Does not trigger if Default::default() is used via another type implementing Default.
- This is a re-open of [PR#4761](#4761), but I couldn't figure out how to re-open that one so here's a new one with the requested changes :S
bors added a commit that referenced this issue Nov 4, 2020
Add lint for 'field_reassign_with_default` #568

changelog: Add lint for field_reassign_with_default that checks if mutable object + field modification is used to edit a binding initialized with Default::default() instead of struct constructor.

Fixes #568

Notes:
- Checks for reassignment of one or more fields of a binding initialized with Default::default().
- Implemented using EarlyLintPass, might be future proofed better with LateLintPass.
- Does not trigger if Default::default() is used via another type implementing Default.
- This is a re-open of [PR#4761](#4761), but I couldn't figure out how to re-open that one so here's a new one with the requested changes :S
@bors bors closed this as completed in 7b203f3 Nov 4, 2020
Ryan1729 pushed a commit to Ryan1729/rust-clippy that referenced this issue Nov 7, 2020
- Implement `field_reassign_with_default` as a `LateLintPass`
- Avoid triggering `default_trait_access` on a span already linted by
`field_reassigned_with_default`
- Merge `default_trait_access` and `field_reassign_with_default` into
`Default`
- Co-authored-by: Eduardo Broto <[email protected]>
- Fixes rust-lang#568
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy T-AST Type: Requires working with the AST
Projects
None yet
2 participants