rust-lang / rust-lang/rust-clippy

Detect assignment operators that do nothing (e.g. `+= 0`)

Open
#16,977 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Currently, identity_op can detect expressions that contain useless operators:

warning: this operation has no effect
 --> src/lib.rs:3:15
  |
3 |     counter = counter + 0;
  |               ^^^^^^^^^^^ help: consider reducing it to: `counter`

However, nothing detects a useless assignment version of the same operation: counter += 0 produces no lint at all. Detecting this seems to me to be a straightforward extension of what identity_op is already doing — though perhaps it should be a separate lint, since identity_op is sometimes a style negative, as in #13813, and this case is much more likely to be an outright bug.

Advantage

Detects code that is unnecessary and may be a bug.

Drawbacks

As with identity_op, the author might want code that completes a regular pattern even if one case is a no-op.

Example
pub fn example() -> i32 {
    let mut counter = 0;
    counter += 0;
    counter
}

Could be written as:

pub fn example() -> i32 {
    let mut counter = 0;
    counter
}

But the diagnostic should focus on the hypothesis that this is a bug, not that the code can be simplified while preserving its behavior.

Comparison with existing lints

No response

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing identity_op lint and its tests, then trace how assignment operators are represented and diagnosed. Add coverage for compound assignments such as counter += 0; done means the useless assignment is reported with a bug-focused diagnostic while preserving the existing behavior for ordinary identity operations.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.