2018-10-06 16:18:06 +00:00
|
|
|
// 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.
|
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
|
|
|
|
#![allow(clippy::if_same_then_else)]
|
2018-06-08 03:55:11 +00:00
|
|
|
|
2018-05-27 22:02:38 +00:00
|
|
|
fn main() {
|
|
|
|
let x = Some(());
|
|
|
|
if x.is_some() {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
} else {
|
|
|
|
x.unwrap(); // will panic
|
2018-05-27 22:02:38 +00:00
|
|
|
}
|
|
|
|
if x.is_none() {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // will panic
|
2018-05-27 22:02:38 +00:00
|
|
|
} else {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // unnecessary
|
2018-05-27 22:02:38 +00:00
|
|
|
}
|
|
|
|
let mut x: Result<(), ()> = Ok(());
|
|
|
|
if x.is_ok() {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
x.unwrap_err(); // will panic
|
2018-05-27 22:02:38 +00:00
|
|
|
} else {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // will panic
|
|
|
|
x.unwrap_err(); // unnecessary
|
2018-05-27 22:02:38 +00:00
|
|
|
}
|
|
|
|
if x.is_err() {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // will panic
|
|
|
|
x.unwrap_err(); // unnecessary
|
2018-05-27 22:02:38 +00:00
|
|
|
} else {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
x.unwrap_err(); // will panic
|
2018-05-27 22:02:38 +00:00
|
|
|
}
|
|
|
|
if x.is_ok() {
|
|
|
|
x = Err(());
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // not unnecessary because of mutation of x
|
2018-12-09 22:26:16 +00:00
|
|
|
// it will always panic but the lint is not smart enough to see this (it only checks if conditions).
|
2018-05-27 22:02:38 +00:00
|
|
|
} else {
|
|
|
|
x = Ok(());
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap_err(); // not unnecessary because of mutation of x
|
2018-12-09 22:26:16 +00:00
|
|
|
// it will always panic but the lint is not smart enough to see this (it only checks if conditions).
|
2018-05-27 22:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_complex_conditions() {
|
|
|
|
let x: Result<(), ()> = Ok(());
|
|
|
|
let y: Result<(), ()> = Ok(());
|
|
|
|
if x.is_ok() && y.is_err() {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
x.unwrap_err(); // will panic
|
|
|
|
y.unwrap(); // will panic
|
|
|
|
y.unwrap_err(); // unnecessary
|
2018-05-27 22:02:38 +00:00
|
|
|
} else {
|
2018-06-08 16:12:01 +00:00
|
|
|
// not statically determinable whether any of the following will always succeed or always fail:
|
|
|
|
x.unwrap();
|
2018-05-27 22:02:38 +00:00
|
|
|
x.unwrap_err();
|
|
|
|
y.unwrap();
|
2018-06-08 16:12:01 +00:00
|
|
|
y.unwrap_err();
|
2018-05-27 22:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if x.is_ok() || y.is_ok() {
|
2018-06-08 16:12:01 +00:00
|
|
|
// not statically determinable whether any of the following will always succeed or always fail:
|
2018-05-27 22:02:38 +00:00
|
|
|
x.unwrap();
|
|
|
|
y.unwrap();
|
|
|
|
} else {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // will panic
|
|
|
|
x.unwrap_err(); // unnecessary
|
|
|
|
y.unwrap(); // will panic
|
|
|
|
y.unwrap_err(); // unnecessary
|
2018-05-27 22:02:38 +00:00
|
|
|
}
|
|
|
|
let z: Result<(), ()> = Ok(());
|
|
|
|
if x.is_ok() && !(y.is_ok() || z.is_err()) {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
x.unwrap_err(); // will panic
|
|
|
|
y.unwrap(); // will panic
|
|
|
|
y.unwrap_err(); // unnecessary
|
|
|
|
z.unwrap(); // unnecessary
|
|
|
|
z.unwrap_err(); // will panic
|
2018-05-27 22:02:38 +00:00
|
|
|
}
|
|
|
|
if x.is_ok() || !(y.is_ok() && z.is_err()) {
|
2018-06-08 16:12:01 +00:00
|
|
|
// not statically determinable whether any of the following will always succeed or always fail:
|
|
|
|
x.unwrap();
|
2018-05-27 22:02:38 +00:00
|
|
|
y.unwrap();
|
2018-06-08 16:12:01 +00:00
|
|
|
z.unwrap();
|
|
|
|
} else {
|
|
|
|
x.unwrap(); // will panic
|
|
|
|
x.unwrap_err(); // unnecessary
|
|
|
|
y.unwrap(); // unnecessary
|
|
|
|
y.unwrap_err(); // will panic
|
|
|
|
z.unwrap(); // will panic
|
|
|
|
z.unwrap_err(); // unnecessary
|
2018-05-27 22:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_nested() {
|
|
|
|
fn nested() {
|
|
|
|
let x = Some(());
|
|
|
|
if x.is_some() {
|
2018-06-08 16:12:01 +00:00
|
|
|
x.unwrap(); // unnecessary
|
|
|
|
} else {
|
|
|
|
x.unwrap(); // will panic
|
2018-05-27 22:02:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|