Skip to content

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

Open
@matthiaskrgr

Description

@matthiaskrgr

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 :(

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.L-unused_assignmentsLint: unused_assignmentsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions