Skip to content

branches_sharing_code incorrectly detects branches using the same code #7369

Closed
@rockstar

Description

@rockstar

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions