Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

branches_sharing_code incorrectly detects branches using the same code #7369

Closed
rockstar opened this issue Jun 17, 2021 · 3 comments · Fixed by #7462
Closed

branches_sharing_code incorrectly detects branches using the same code #7369

rockstar opened this issue Jun 17, 2021 · 3 comments · Fixed by #7462
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@rockstar
Copy link

rockstar commented Jun 17, 2021

Lint name: branches_sharing_code

I upgrade to rust 1.53 and tried this code:

    fn format_node_with_parens(&mut self, node: &Node) {
        if has_parens(node) {
            // If the AST already has parens here do not double add them
            self.format_node(node);
        } else {
            self.write_rune('(');
            self.format_node(node);
            self.write_rune(')')
        }
    }

I expected clippy to be fine with this code.

Instead, this this code was linted incorrectly with the following:

error: all if blocks contain the same code at the end
   --> flux-core/src/formatter/mod.rs:799:9
    |
799 | /             self.format_node(node);
800 | |             self.write_rune(')')
801 | |         }
    | |_________^
    |
note: the lint level is defined here
   --> flux-core/src/lib.rs:1:38
    |
1   | #![cfg_attr(feature = "strict", deny(warnings, missing_docs))]
    |                                      ^^^^^^^^
    = note: `#[deny(clippy::branches_sharing_code)]` implied by `#[deny(warnings)]`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
help: consider moving the end statements out like this
    |
799 |         }
800 |         self.format_node(node);
801 |         self.write_rune(')')
    |

error: all if blocks contain the same code at the end
   --> flux-core/src/formatter/mod.rs:799:9
    |
799 | /             self.format_node(node);
800 | |             self.write_rune(')')
801 | |         }
    | |_________^
    |
note: the lint level is defined here
   --> flux-core/src/lib.rs:1:38
    |
1   | #![cfg_attr(feature = "strict", deny(warnings, missing_docs))]
    |                                      ^^^^^^^^
    = note: `#[deny(clippy::branches_sharing_code)]` implied by `#[deny(warnings)]`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
help: consider moving the end statements out like this
    |
799 |         }
800 |         self.format_node(node);
801 |         self.write_rune(')')
    |

The suggested code changes what the code actually does, rather than just finding a better way to express them. In this case, if we made the change, we'd change the functionality and (thankfully) break tests asserting specific behavior.

Meta

  • cargo clippy -V: clippy 0.1.53 (53cb7b09 2021-06-17)
  • rustc -Vv:
  rustc 1.53.0 (53cb7b09b 2021-06-17)
binary: rustc
commit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b
commit-date: 2021-06-17
host: x86_64-apple-darwin
release: 1.53.0
LLVM version: 12.0.1
@rockstar rockstar added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jun 17, 2021
@xFrednet
Copy link
Member

Hey, thank you for the report! Could you retest this false positive? I tried a quick reproduction in the playground using print statements, but this didn't trigger the lint.

@jix
Copy link

jix commented Jul 13, 2021

I ran into this bug a few hours ago and just confirmed that it is still in the nightly clippy 0.1.55 (955b9c0 2021-07-12).

I reduced my code that triggers this bug down to this in the playground.

@xFrednet
Copy link
Member

Awesome, thank you very much, I'll have a look at it :)

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants