Skip to content

unused_assignments on MIR warns on earlier compound assignment in a read-modify-write chain #154457

@chenyukang

Description

@chenyukang

I tried this code:

fn main() {
    let mut a = 10.;
    let b = 13.;
    let c = 11.;

    a += b;
    a -= c;
}

I expected only the second assignment (a -= c) to trigger unused_assignments, matching old stable behavior (rustc 1.91.1 (ed61e7d 2025-11-07)).

The second compound assignment reads the previous value of a, so the value produced by a += b appears to be used as part of evaluating a -= c.

Instead, this happened:

warning: value assigned to `a` is never read
 --> /tmp/haha.rs:6:5
  |
6 |     a += b;
  |     ^^^^^^
  |
  = help: maybe it is overwritten before being read?

warning: value assigned to `a` is never read
 --> /tmp/haha.rs:7:5
  |
7 |     a -= c;
  |     ^^^^^^
  |
  = help: maybe it is overwritten before being read?

Find this when working on #154456

The question here is about the lint result itself: when a compound assignment is immediately followed by another compound assignment on the same place, should the earlier assignment still be considered "never read", even though the later operation reads the old value as part of its read-modify-write semantics?

corresponding test case:

fn f9() {
let mut a = 10;
//~^ ERROR variable `a` is assigned to, but never used
let b = 13;
let c = 13;
let d = 13;
let e = 13;
let f = 13;
let g = 13;
let h = 13;
a += b;
//~^ ERROR value assigned to `a` is never read
a -= c;
//~^ ERROR value assigned to `a` is never read
a *= d;
//~^ ERROR value assigned to `a` is never read
a /= e;
//~^ ERROR value assigned to `a` is never read
a |= f;
//~^ ERROR value assigned to `a` is never read
a &= g;
//~^ ERROR value assigned to `a` is never read
a %= h;
//~^ ERROR value assigned to `a` is never read
}

Metadata

Metadata

Assignees

Labels

A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlC-bugCategory: This is a bug.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions