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

Superfluous assignment does not give warning #75356

Closed
alvinhochun opened this issue Aug 10, 2020 · 1 comment · Fixed by #87129
Closed

Superfluous assignment does not give warning #75356

alvinhochun opened this issue Aug 10, 2020 · 1 comment · Fixed by #87129
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@alvinhochun
Copy link

This code (playground):

#[derive(Clone, Copy, Debug)]
struct Boo {
    b: i32,
}

fn main() {
    let mut a = 1i32;
    dbg!(a);
    // Warn?
    a = a;
    dbg!(a);

    let mut boo = Boo { b: 1 };
    dbg!(boo);
    // Warn?
    boo.b = boo.b;
    dbg!(boo);
}

contains superfluous self-assignments (a = a and boo.b = boo.b). These assignments are useless (especially so in Rust since assignment operations cannot be overloaded) and almost always means incorrect code.

However, rustc does not give a warning at all. Even clippy doesn't.

This broken code for example went unnoticed for years: https://github.com/PistonDevelopers/conrod/pull/1377/files

Examples in other compilers/linters:

Clang gives warning: explicitly assigning value of variable of type 'int' to itself [-Wself-assign] on variable self-assignment. It does not, however, warn about struct field self-assignment. GCC gives no warnings whatsoever. (Compiler Explorer)

go vet gives a warning self-assignment of [*] to [*] on variable and struct field self-assignmet.

eslint has the lint no-self-assign which works for both variables and object properties.

@alvinhochun alvinhochun added the C-bug Category: This is a bug. label Aug 10, 2020
@lcnr lcnr added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Aug 10, 2020
@varkor
Copy link
Member

varkor commented Sep 28, 2020

It seems reasonable that this should be covered by the dead_code lint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants