rust-lang / rust-lang/rust-clippy

arithmetic_side_effects: false positive with inline const blocks

Open
#17,263 1 comment 0 reactions 1 assignee View on GitHub

@profetia is already working on this.

Since Jun 22, 2026.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

arithmetic_side_effects lint is able to check some syntactic structures for division by a constant integer and avoid linting when this integer is known to not be zero. However, this seems to fail for inline const {} blocks which contain expressions equivalent to those used to compute a proper constant...

Lint Name

arithmetic_side_effects

Reproducer

I tried this code:

#![deny(clippy::arithmetic_side_effects)]

pub fn correct1(f: u64) -> u64 {
    const SZ: u64 = std::mem::size_of::<u64>() as u64; 
    f.saturating_div(SZ)
}

pub fn correct2(f: u64) -> u64 {
    const SZ: u64 = 0;
    f.saturating_div(SZ)
}

pub fn correct3(f: u64) -> u64 {
    const SZ: u64 = std::mem::size_of::<u64>() as u64; 
    f.saturating_div(const { SZ })
}

pub fn false_positive(f: u64) -> u64 {
    f.saturating_div(const { std::mem::size_of::<u64>() as u64 })
}

I saw this happen:

error: arithmetic operation that can potentially result in unexpected side-effects
  --> src/lib.rs:10:22
   |
10 |     f.saturating_div(SZ)
   |                      ^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
 1 | #![deny(clippy::arithmetic_side_effects)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: arithmetic operation that can potentially result in unexpected side-effects
  --> src/lib.rs:19:22
   |
19 |     f.saturating_div(const { std::mem::size_of::<u64>() as u64 })
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects

I expected to see this happen:

The const { ... } variant should not trigger the lint.

Version
1.98.0-nightly (2026-06-17 c1b22f44c3f30e186b82)
Additional Labels

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.