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.
11 #![feature(tool_lints)]
13 #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
14 #![allow(clippy::if_same_then_else)]
19 x.unwrap(); // unnecessary
21 x.unwrap(); // will panic
24 x.unwrap(); // will panic
26 x.unwrap(); // unnecessary
28 let mut x: Result<(), ()> = Ok(());
30 x.unwrap(); // unnecessary
31 x.unwrap_err(); // will panic
33 x.unwrap(); // will panic
34 x.unwrap_err(); // unnecessary
37 x.unwrap(); // will panic
38 x.unwrap_err(); // unnecessary
40 x.unwrap(); // unnecessary
41 x.unwrap_err(); // will panic
45 x.unwrap(); // not unnecessary because of mutation of x
46 // it will always panic but the lint is not smart enough to see this (it only checks if conditions).
49 x.unwrap_err(); // not unnecessary because of mutation of x
50 // it will always panic but the lint is not smart enough to see this (it only checks if conditions).
54 fn test_complex_conditions() {
55 let x: Result<(), ()> = Ok(());
56 let y: Result<(), ()> = Ok(());
57 if x.is_ok() && y.is_err() {
58 x.unwrap(); // unnecessary
59 x.unwrap_err(); // will panic
60 y.unwrap(); // will panic
61 y.unwrap_err(); // unnecessary
63 // not statically determinable whether any of the following will always succeed or always fail:
70 if x.is_ok() || y.is_ok() {
71 // not statically determinable whether any of the following will always succeed or always fail:
75 x.unwrap(); // will panic
76 x.unwrap_err(); // unnecessary
77 y.unwrap(); // will panic
78 y.unwrap_err(); // unnecessary
80 let z: Result<(), ()> = Ok(());
81 if x.is_ok() && !(y.is_ok() || z.is_err()) {
82 x.unwrap(); // unnecessary
83 x.unwrap_err(); // will panic
84 y.unwrap(); // will panic
85 y.unwrap_err(); // unnecessary
86 z.unwrap(); // unnecessary
87 z.unwrap_err(); // will panic
89 if x.is_ok() || !(y.is_ok() && z.is_err()) {
90 // not statically determinable whether any of the following will always succeed or always fail:
95 x.unwrap(); // will panic
96 x.unwrap_err(); // unnecessary
97 y.unwrap(); // unnecessary
98 y.unwrap_err(); // will panic
99 z.unwrap(); // will panic
100 z.unwrap_err(); // unnecessary
108 x.unwrap(); // unnecessary
110 x.unwrap(); // will panic