rust-lang / rust-lang/rust-clippy

identity_op: span could point at the redundant expression instead of the rest of it?

Open
#9,537 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Description
fn main() {
    let _ =  (1 + 2) * 1 - 1;
}

warning:

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

it's a bit weird that clippy says "this operation has no effect" and then underlines everything BUT the operation that has no effect... 😅

would it make sense to underline the * 1 instead?

Perhaps this newer fancier diff-like diagnostic format could be used here?

-    let _ =  (1 + 2) * 1 - 1;
                      ~~~  // this part ^ would be red
+   let _ =  (1 + 2)      - 1;
Version
clippy 0.1.65 (3f83906 2022-09-24)
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 at the identity_op lint entry point and reproduce the diagnostic with the Rust example in this issue. Adjust the reported span so it highlights the redundant * 1 operation rather than the surrounding expression, then verify that the warning and suggested reduction use the intended range.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.