Skip to content

False positive for if_same_then_else #3559

Closed
@Vzaa

Description

@Vzaa

Hello,

I was using clippy to check some code I wrote for advent of code 2018 and I believe I have a false positive for if_same_then_else.

The piece of code I get the error for:

use std::collections::HashSet;

fn main() {
    let text = "x\ny\n";
    let mut clays = HashSet::new();

    for line in text.lines() {
        let (from, to, p) = (10, 20, 10);

        if line.starts_with('x') {
            let x = p;
            for y in from..=to {
                clays.insert((x, y));
            }
        } else if line.starts_with('y') {
            let y = p;
            for x in from..=to {
                clays.insert((x, y));
            }
        }
    }
}

cargo clippy output:

$ cargo clippy                                                                                                                                                                                                                      (master ?)
    Checking cl v0.1.0 (/tmp/cl)                                                                                                                                                                                                               
error: this `if` has identical blocks                                                                                                                                                                                                          
  --> src/main.rs:15:41                                                                                                                                                                                                                        
   |                                                                                                                                                                                                                                           
15 |           } else if line.starts_with('y') {                                                                                                                                                                                               
   |  _________________________________________^                                                                                                                                                                                               
16 | |             let y = p;                                                                                                                                                                                                                  
17 | |             for x in from..=to {                                                                                                                                                                                                        
18 | |                 clays.insert((x, y));                                                                                                                                                                                                   
19 | |             }                                                                                                                                                                                                                           
20 | |         }                                                                                                                                                                                                                               
   | |_________^                                                                                                                                                                                                                               
   |                                                                                                                                                                                                                                           
   = note: #[deny(clippy::if_same_then_else)] on by default                                                                                                                                                                                    
note: same as this                                                                                                                                                                                                                             
  --> src/main.rs:10:34                                                                                                                                                                                                                        
   |                                                                                                                                                                                                                                           
10 |           if line.starts_with('x') {                                                                                                                                                                                                      
   |  __________________________________^                                                                                                                                                                                                      
11 | |             let x = p;                                                                                                                                                                                                                  
12 | |             for y in from..=to {                                                                                                                                                                                                        
13 | |                 clays.insert((x, y));                                                                                                                                                                                                   
14 | |             }                                                                                                                                                                                                                           
15 | |         } else if line.starts_with('y') {                                                                                                                                                                                               
   | |_________^                                                                                                                                                                                                                               
   = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/master/index.html#if_same_then_else                                                                                                                   
                                                                                                                                                                                                                                               
error: aborting due to previous error                                                                                                                                                                                                          
                                                                                                                                                                                                                                               
error: Could not compile `cl`.   

In the code it either iterates fromfrom to to for x or y while p determines the stationary point. The blocks actually have different outcomes.

cargo clippy -V output:

$ cargo clippy -V                                                                                                                                                                                                                   (master ?)
clippy 0.0.212 (2e26fdc 2018-11-22)

The same error is reported on the version on Rust Playground:
Playground Link

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions