a = a op b written out instead of the compound assignment a op= b

ID

rust.maintainability.assign_op_pattern

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Idiom

Language

Rust

Description

Reports an assignment whose right-hand side applies a binary operator to the assignment target itself - a = a + b, mask = mask | flag, self.total = self.total * 2 - where the compound operator (+=, |=, *=, …​) says the same thing once.

Rationale

Spelling the target twice makes the reader compare the two sides to confirm they are the same place, and any mismatch between them is a bug the compiler will not catch - a = b + a and total = other.total + by look almost identical to the correct form. When the target is an index or a field access, the long form also repeats a subexpression that must stay in step.

The following code illustrates the pattern detected by this rule:

pub fn arithmetic(mut a: i32, b: i32) -> i32 {
    // FLAGGED: `a = a op b` written out instead of the compound assignment `a op= b`
    a = a + b;
    // FLAGGED: `a = a op b` written out instead of the compound assignment `a op= b`
    a = a - b;
    // FLAGGED: `a = a op b` written out instead of the compound assignment `a op= b`
    a = a * 2;
    // FLAGGED: `a = a op b` written out instead of the compound assignment `a op= b`
    a = a / 2;
    // FLAGGED: `a = a op b` written out instead of the compound assignment `a op= b`
    a = a % 3;
    a
}

Remediation

Use the compound operator: a = a + b becomes a += b, mask = mask | flag becomes mask |= flag. The operand types are unchanged, since every arithmetic and bitwise operator in the standard library ships the matching *Assign trait.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved