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

False positive branches_sharing_code: different inferred type #7053

Closed
dtolnay opened this issue Apr 9, 2021 · 1 comment · Fixed by #7075
Closed

False positive branches_sharing_code: different inferred type #7053

dtolnay opened this issue Apr 9, 2021 · 1 comment · Fixed by #7075
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

@dtolnay
Copy link
Member

dtolnay commented Apr 9, 2021

fn main() {
    if true {
        let mut v = Vec::new();
        v.push(0);
    } else {
        let mut v = Vec::new();
        v.push("");
    };
}
$ cargo clippy
warning: all if blocks contain the same code at the start
 --> src/main.rs:2:5
  |
2 | /     if true {
3 | |         let mut v = Vec::new();
  | |_______________________________^
  |
  = note: `#[warn(clippy::branches_sharing_code)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
help: consider moving the start statements out like this
  |
2 |     let mut v = Vec::new();
3 |     if true {
  |

Clippy's preferred code is the following, which does not compile.

fn main() {
    let mut v = Vec::new();
    if true {
        v.push(0);
    } else {
        v.push("");
    };
}
error[E0308]: mismatched types
 --> src/main.rs:6:16
  |
6 |         v.push("");
  |                ^^ expected integer, found `&str`

Meta

  • cargo clippy -V: clippy 0.1.53 (2e495d2 2021-04-08)
  • rustc -Vv:
    rustc 1.53.0-nightly (2e495d2e8 2021-04-08)
    binary: rustc
    commit-hash: 2e495d2e845cf27740e3665f718acfd3aa17253e
    commit-date: 2021-04-08
    host: x86_64-unknown-linux-gnu
    release: 1.53.0-nightly
    LLVM version: 12.0.0
    

Mentioning @xFrednet @phansch #6463

@dtolnay dtolnay 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 Apr 9, 2021
dtolnay added a commit to dtolnay/cargo-tally that referenced this issue Apr 9, 2021
rust-lang/rust-clippy#7053

    warning: all if blocks contain the same code at the start and the end. Here at the start
      --> src/graph.rs:58:13
       |
    58 | /             if args.relative {
    59 | |                 let mut y = Vec::new();
       | |_______________________________________^
       |
       = note: `#[warn(clippy::branches_sharing_code)]` on by default
    note: and here at the end
      --> src/graph.rs:73:13
       |
    73 | /                 axes.lines(
    74 | |                     &x,
    75 | |                     &y,
    76 | |                     &[Caption(&captions[i]), LineWidth(1.5), Color(&colors[i])],
    77 | |                 );
    78 | |             }
       | |_____________^
       = warning: Some moved values might need to be renamed to avoid wrong references
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
    help: consider moving the start statements out like this
       |
    58 |             let mut y = Vec::new();
    59 |             if args.relative {
       |
    help: and consider moving the end statements out like this
       |
    73 |             }
    74 |             axes.lines(
    75 |                 &x,
    76 |                 &y,
    77 |                 &[Caption(&captions[i]), LineWidth(1.5), Color(&colors[i])],
    78 |             );
       |
@xFrednet
Copy link
Member

xFrednet commented Apr 9, 2021

This is very interesting and sounds like a bug that is caused by the comparison function but i would need to dig into that.

Thank you for pinging me directly :)

@phansch should I submit a temporary PR to move this lint back to nursery in the mean time since it has only been in nightly so far? :)

@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.

2 participants