Skip to content

Superfluous assignment does not give warning #75356

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

Open
alvinhochun opened this issue Aug 10, 2020 · 2 comments · Fixed by #87129
Open

Superfluous assignment does not give warning #75356

alvinhochun opened this issue Aug 10, 2020 · 2 comments · Fixed by #87129
Labels
A-lints 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. L-dead_code Lint: dead_code T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

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-lints 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.

@bors bors closed this as completed in eb0b95b Jul 18, 2021
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. L-dead_code Lint: dead_code labels Apr 13, 2025
@jieyouxu
Copy link
Member

jieyouxu commented Apr 13, 2025

Reopening because the implementation #87129 unfortunately had to be disabled as part of reverts #86212, #83171 to address regressions #81626 and #81658 (see revert summary #81473 (comment)).

@jieyouxu jieyouxu reopened this Apr 13, 2025
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. C-enhancement Category: An issue proposing an enhancement or a PR with one. L-dead_code Lint: dead_code T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants