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

unused_assignments warning: false negative for dead stores in struct fields #49256

Open
matthiaskrgr opened this issue Mar 22, 2018 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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-unused_assignments Lint: unused_assignments T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Mar 22, 2018

code like this causes a proper warning

fn main() {
	let mut a;
	let b = 4;
	let c = 5;

    	a = b;
    	a = c;

	println!("{}", a);
}

=>

warning: value assigned to `a` is never read
 --> src/main.rs:6:5
  |
6 |     a = b;
  |     ^
  |
  = note: #[warn(unused_assignments)] on by default

however if a is a struct field, I get no warning at all.

struct Foo {
    x: i32,
}

fn main() {
	let mut strct = Foo {
	    x: 0, // maybe warn here, too?
	};
	let b = 4;
	let c = 5;

  	strct.x = b; // please warn!
   	strct.x = c;

	println!("{}", strct.x);
}

playground: https://play.rust-lang.org/?gist=c904ff202754b2691e2968977620fd7a&version=nightly

I ran into bugs in my code that could have been prevented by warning about this :(

@matthiaskrgr matthiaskrgr changed the title unused_assignments: false negative for dead stores in struct fields unused_assignments warning: false negative for dead stores in struct fields Mar 22, 2018
@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints labels Mar 27, 2018
@Boscop
Copy link

Boscop commented Apr 13, 2018

I also ran into a bug where it was a situation like this, also no warning that a is never read:

struct Foo {
    a: i32
}

impl Foo {
    fn bar(&mut self) {
        self.a = 42;
    }
}

fn main() {
    let mut foo = Foo { a: 1 };
    foo.bar();
}

https://play.rust-lang.org/?gist=dbe81aa3431e467557fc2bf930827c52&version=stable

@jonas-schievink jonas-schievink added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 20, 2020
JohnTitor added a commit to JohnTitor/rust that referenced this issue Jan 30, 2021
@fmease fmease added the L-unused_assignments Lint: unused_assignments label Aug 30, 2024
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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-unused_assignments Lint: unused_assignments 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

5 participants