Skip to content

Assign op pattern should not apply in const context. #14871

@Wasabi375

Description

@Wasabi375

Summary

Right now only a handful of traits are actually const traits. One of them is Add. However AddAssign is not one of them. Therefor
the statement a = a + some_constant is not equal to a += some_constant.

Lint Name

assign_op_pattern

Reproducer

I tried this code:

impl<T> const NumberConstants for T
where
    T: Number + ~const core::ops::Add,
{
    fn constant(value: usize) -> Self {
        let mut res = Self::ZERO;
        let mut count = 0;
        while count < value {
            res = res + Self::ONE;
            count += 1;
        }
        res
    }
}
<code>

I saw this happen:

res += Self::ONE;

I expected to see this happen:
Clippy should not apply this pattern when in a const context.

Version

rustc 1.89.0-nightly (bc8215286 2025-05-20)
binary: rustc
commit-hash: bc821528634632b4ff8dee5ac1ea4ad90d1b3eb5
commit-date: 2025-05-20
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions