rust/tests/ui/if_same_then_else2.rs
Matthias Krüger 6dcec6ae86 collapsible_if: split collapsible_else_if into its own lint so we can enable/disable it particularly
This splits up clippy::collapsible_if into collapsible_if for
if x {
  if y { }
}
=>
if x && y { }

and collapsible_else_if for

if x {
} else {
 if y { }
}

=>
if x {

} else if y {

}

so that we can lint for only the latter but not the first if we desire.

changelog: collapsible_if: split up linting for if x {} else { if y {} } into collapsible_else_if lint
2021-01-04 13:34:14 +01:00

141 lines
2.8 KiB
Rust

#![warn(clippy::if_same_then_else)]
#![allow(
clippy::blacklisted_name,
clippy::collapsible_else_if,
clippy::collapsible_if,
clippy::ifs_same_cond,
clippy::needless_return,
clippy::single_element_loop
)]
fn if_same_then_else2() -> Result<&'static str, ()> {
if true {
for _ in &[42] {
let foo: &Option<_> = &Some::<u8>(42);
if true {
break;
} else {
continue;
}
}
} else {
//~ ERROR same body as `if` block
for _ in &[42] {
let foo: &Option<_> = &Some::<u8>(42);
if true {
break;
} else {
continue;
}
}
}
if true {
if let Some(a) = Some(42) {}
} else {
//~ ERROR same body as `if` block
if let Some(a) = Some(42) {}
}
if true {
if let (1, .., 3) = (1, 2, 3) {}
} else {
//~ ERROR same body as `if` block
if let (1, .., 3) = (1, 2, 3) {}
}
if true {
if let (1, .., 3) = (1, 2, 3) {}
} else {
if let (.., 3) = (1, 2, 3) {}
}
if true {
if let (1, .., 3) = (1, 2, 3) {}
} else {
if let (.., 4) = (1, 2, 3) {}
}
if true {
if let (1, .., 3) = (1, 2, 3) {}
} else {
if let (.., 1, 3) = (1, 2, 3) {}
}
if true {
if let Some(42) = None {}
} else {
if let Option::Some(42) = None {}
}
if true {
if let Some(42) = None::<u8> {}
} else {
if let Some(42) = None {}
}
if true {
if let Some(42) = None::<u8> {}
} else {
if let Some(42) = None::<u32> {}
}
if true {
if let Some(a) = Some(42) {}
} else {
if let Some(a) = Some(43) {}
}
// Same NaNs
let _ = if true {
f32::NAN
} else {
//~ ERROR same body as `if` block
f32::NAN
};
if true {
Ok("foo")?;
} else {
//~ ERROR same body as `if` block
Ok("foo")?;
}
if true {
let foo = "";
return Ok(&foo[0..]);
} else if false {
let foo = "bar";
return Ok(&foo[0..]);
} else {
let foo = "";
return Ok(&foo[0..]);
}
if true {
let foo = "";
return Ok(&foo[0..]);
} else if false {
let foo = "bar";
return Ok(&foo[0..]);
} else if true {
let foo = "";
return Ok(&foo[0..]);
} else {
let foo = "";
return Ok(&foo[0..]);
}
// False positive `if_same_then_else`: `let (x, y)` vs. `let (y, x)`; see issue #3559.
if true {
let foo = "";
let (x, y) = (1, 2);
return Ok(&foo[x..y]);
} else {
let foo = "";
let (y, x) = (1, 2);
return Ok(&foo[x..y]);
}
}
fn main() {}