1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
11 #![allow(clippy::if_same_then_else)]
16 x.unwrap(); // unnecessary
18 x.unwrap(); // will panic
21 x.unwrap(); // will panic
23 x.unwrap(); // unnecessary
25 let mut x: Result<(), ()> = Ok(());
27 x.unwrap(); // unnecessary
28 x.unwrap_err(); // will panic
30 x.unwrap(); // will panic
31 x.unwrap_err(); // unnecessary
34 x.unwrap(); // will panic
35 x.unwrap_err(); // unnecessary
37 x.unwrap(); // unnecessary
38 x.unwrap_err(); // will panic
42 x.unwrap(); // not unnecessary because of mutation of x
43 // it will always panic but the lint is not smart enough to see this (it only checks if conditions).
46 x.unwrap_err(); // not unnecessary because of mutation of x
47 // it will always panic but the lint is not smart enough to see this (it only checks if conditions).
51 fn test_complex_conditions() {
52 let x: Result<(), ()> = Ok(());
53 let y: Result<(), ()> = Ok(());
54 if x.is_ok() && y.is_err() {
55 x.unwrap(); // unnecessary
56 x.unwrap_err(); // will panic
57 y.unwrap(); // will panic
58 y.unwrap_err(); // unnecessary
60 // not statically determinable whether any of the following will always succeed or always fail:
67 if x.is_ok() || y.is_ok() {
68 // not statically determinable whether any of the following will always succeed or always fail:
72 x.unwrap(); // will panic
73 x.unwrap_err(); // unnecessary
74 y.unwrap(); // will panic
75 y.unwrap_err(); // unnecessary
77 let z: Result<(), ()> = Ok(());
78 if x.is_ok() && !(y.is_ok() || z.is_err()) {
79 x.unwrap(); // unnecessary
80 x.unwrap_err(); // will panic
81 y.unwrap(); // will panic
82 y.unwrap_err(); // unnecessary
83 z.unwrap(); // unnecessary
84 z.unwrap_err(); // will panic
86 if x.is_ok() || !(y.is_ok() && z.is_err()) {
87 // not statically determinable whether any of the following will always succeed or always fail:
92 x.unwrap(); // will panic
93 x.unwrap_err(); // unnecessary
94 y.unwrap(); // unnecessary
95 y.unwrap_err(); // will panic
96 z.unwrap(); // will panic
97 z.unwrap_err(); // unnecessary
105 x.unwrap(); // unnecessary
107 x.unwrap(); // will panic