Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::misrefactored_assign_op: variable appears on both sides of an assignment operation
What it meansChecks for a op= a op b or a op= b op a patterns. It is in the suspicious group and warns by default.
What you see
warning: variable appears on both sides of an assignment operation --> main.rs:6:1 | 6 | a += a + b; | ^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#misrefactored_assign_op = note: `#[warn(clippy::misrefactored_assign_op)]` on by default help: did you mean `a = a + b` or `a = a + a + b`? Consider replacing it with | 6 - a += a + b; 6 + a += b; | help: or | 6 - a += a + b; 6 + a = a + a + b; |
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Most likely these are bugs where one meant to write a op= b.
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let mut a = 5;
let b = 2;
// ...
a += a + b;
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::misrefactored_assign_op)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
misrefactored_assign_op = "allow"One run
cargo clippy -- -A clippy::misrefactored-assign-opThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for a op= a op b or a op= b op a patterns.
Why is this bad?
Most likely these are bugs where one meant to write a op= b.
Known problems
Clippy cannot know for sure if a op= a op b should have
been a = a op a op b or a = a op b/a op= b. Therefore, it suggests both.
If a op= a op b is really the correct behavior it should be
written as a = a op a op b as it’s less confusing.
Example
let mut a = 5;
let b = 2;
// ...
a += a + b;
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- manual_unwrap_or_defaultmatch can be simplified with `.unwrap_or_default()`
- mismatched_bit_width_typepossible buggy implementation of `bit_width`
- misnamed_gettersgetter function appears to return the wrong field
- missing_transmute_annotationsChecks if transmute calls have all generics specified
- multi_assignmentsassignments don't nest intuitively
- mut_range_boundattempt to mutate range bound within loop
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::manual_unwrap_or_defaultmatch can be simplified with `.unwrap_or_default()`Read it next →Did this answer your question?