Search code examples
rustshadowingrust-clippy

Bad enforcement of clippy rule?


I have disallowed unrelated shadowing in variable declarations.

But now this rule gives me an error on these two lines

let overflow: bool;
(self.standing, overflow) = self.standing.overflowing_add(reason.to_severity());

The linting error I get is:

error: `overflow` shadows a previous, unrelated binding
  --> src/models/peer.rs:73:25
   |
73 |         (self.standing, overflow) = self.standing.overflowing_add(reason.to_severity());
   |                         ^^^^^^^^
   |
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
1  | #![deny(clippy::shadow_unrelated)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^
note: previous binding is here
  --> src/models/peer.rs:73:10
   |
73 |         (self.standing, overflow) = self.standing.overflowing_add(reason.to_severity());
   |          ^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#shadow_unrelated

I'm using version 1.62.0 of the rust compiler.

My thoughts are that this behavior is a bug. The above lines should be allowed under this clippy rule. Am I wrong?

The problem can be seen here. Thanks to @Jmp for writing this illustration of the problem.


Solution

  • This is a reported bug - rust-clippy#6141.

    I think the problem is that the destructuring assignment is desugared into a let declaration that reuses the same span, and this causes clippy to think it has the same name and thus shadowing.