-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-
-
-
+// run-rustfix
+#![allow(clippy::assertions_on_constants)]
+#[rustfmt::skip]
#[warn(clippy::collapsible_if)]
fn main() {
let x = "hello";
}
}
- // Collapse `else { if .. }` to `else if ..`
- if x == "hello" {
- print!("Hello ");
- } else {
- if y == "world" {
- println!("world!")
- }
- }
-
- if x == "hello" {
- print!("Hello ");
- } else {
- if let Some(42) = Some(42) {
- println!("world!")
- }
- }
-
- if x == "hello" {
- print!("Hello ");
- } else {
- if y == "world" {
- println!("world")
- }
- else {
- println!("!")
- }
- }
-
- if x == "hello" {
- print!("Hello ");
- } else {
- if let Some(42) = Some(42) {
- println!("world")
- }
- else {
- println!("!")
- }
- }
-
- if let Some(42) = Some(42) {
- print!("Hello ");
- } else {
- if let Some(42) = Some(42) {
- println!("world")
- }
- else {
- println!("!")
- }
- }
-
- if let Some(42) = Some(42) {
- print!("Hello ");
- } else {
- if x == "hello" {
- println!("world")
- }
- else {
- println!("!")
- }
- }
-
- if let Some(42) = Some(42) {
- print!("Hello ");
- } else {
- if let Some(42) = Some(42) {
- println!("world")
- }
- else {
- println!("!")
- }
- }
-
// Works because any if with an else statement cannot be collapsed.
if x == "hello" {
if y == "world" {
}
- // The following tests check for the fix of https://github.com/rust-lang-nursery/rust-clippy/issues/798
+ // The following tests check for the fix of https://github.com/rust-lang/rust-clippy/issues/798
if x == "hello" {// Not collapsible
if y == "world" {
println!("Hello world!");
println!("Hello world!");
}
}
+
+ // Test behavior wrt. `let_chains`.
+ // None of the cases below should be collapsed.
+ fn truth() -> bool { true }
+
+ // Prefix:
+ if let 0 = 1 {
+ if truth() {}
+ }
+
+ // Suffix:
+ if truth() {
+ if let 0 = 1 {}
+ }
+
+ // Midfix:
+ if truth() {
+ if let 0 = 1 {
+ if truth() {}
+ }
+ }
}