rust-lang / rust-lang/rust-clippy

Incorrect identity_op, erasing_op lints when doing arithmetic with seq_macro counters

Open
#10,230 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

Clippy produces incorrect identity_op and erasing_op lints when encountering arithmetic with seq! macro counters. Looks like it incorrectly infers the counter to be constant (which it technically is, but the code gets expanded multiple times with different constant values).

Lint Name

identity_op, erasing_op

Reproducer
use seq_macro::seq;

fn main() {
    let numbers: [usize; 2] = seq!( N in 0..2 {
      [
        #( N * 2, )*
      ]
    } );
    assert_eq!(numbers[0], 0);
    assert_eq!(numbers[1], 2);
}

Clippy output:

    Checking clippy_seq_macro v0.1.0 (/home/mnissler/src/clippy_seq_macro)
error: this operation will always return zero. This is likely not the intended outcome
 --> src/main.rs:6:12
  |
6 |         #( N * 2, )*
  |            ^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#erasing_op
  = note: `#[deny(clippy::erasing_op)]` on by default

warning: this operation has no effect
 --> src/main.rs:6:12
  |
6 |         #( N * 2, )*
  |            ^^^^^ help: consider reducing it to: `2`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
  = note: `#[warn(clippy::identity_op)]` on by default

warning: `clippy_seq_macro` (bin "clippy_seq_macro") generated 1 warning
error: could not compile `clippy_seq_macro` due to previous error; 1 warning emitted
Version
rustc 1.69.0-nightly (c18a5e8a5 2023-01-25)
binary: rustc
commit-hash: c18a5e8a5b1afb0d7a582fe9ebad4c1996c90da3
commit-date: 2023-01-25
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7
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.

Research direction

Start by running the Rust reproducer in src/main.rs and compare the seq! expansion with Clippy's identity_op and erasing_op diagnostics. Done means the arithmetic with seq_macro counters no longer receives incorrect lints, while the provided assertions continue to pass.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.