]> git.lizzy.rs Git - rust.git/blob - tests/ui/redundant_pattern_matching_result.fixed
e94c5704b48917fd89667bed89cd3bb52896ab98
[rust.git] / tests / ui / redundant_pattern_matching_result.fixed
1 // run-rustfix
2
3 #![warn(clippy::all)]
4 #![warn(clippy::redundant_pattern_matching)]
5 #![allow(
6     unused_must_use,
7     clippy::needless_bool,
8     clippy::match_like_matches_macro,
9     clippy::unnecessary_wraps,
10     deprecated
11 )]
12
13 fn main() {
14     let result: Result<usize, usize> = Err(5);
15     if result.is_ok() {}
16
17     if Ok::<i32, i32>(42).is_ok() {}
18
19     if Err::<i32, i32>(42).is_err() {}
20
21     while Ok::<i32, i32>(10).is_ok() {}
22
23     while Ok::<i32, i32>(10).is_err() {}
24
25     if Ok::<i32, i32>(42).is_ok() {}
26
27     if Err::<i32, i32>(42).is_err() {}
28
29     if let Ok(x) = Ok::<i32, i32>(42) {
30         println!("{}", x);
31     }
32
33     Ok::<i32, i32>(42).is_ok();
34
35     Ok::<i32, i32>(42).is_err();
36
37     Err::<i32, i32>(42).is_err();
38
39     Err::<i32, i32>(42).is_ok();
40
41     let _ = if Ok::<usize, ()>(4).is_ok() { true } else { false };
42
43     issue5504();
44     issue6067();
45     issue6065();
46
47     let _ = if gen_res().is_ok() {
48         1
49     } else if gen_res().is_err() {
50         2
51     } else {
52         3
53     };
54 }
55
56 fn gen_res() -> Result<(), ()> {
57     Ok(())
58 }
59
60 macro_rules! m {
61     () => {
62         Some(42u32)
63     };
64 }
65
66 fn issue5504() {
67     fn result_opt() -> Result<Option<i32>, i32> {
68         Err(42)
69     }
70
71     fn try_result_opt() -> Result<i32, i32> {
72         while r#try!(result_opt()).is_some() {}
73         if r#try!(result_opt()).is_some() {}
74         Ok(42)
75     }
76
77     try_result_opt();
78
79     if m!().is_some() {}
80     while m!().is_some() {}
81 }
82
83 fn issue6065() {
84     macro_rules! if_let_in_macro {
85         ($pat:pat, $x:expr) => {
86             if let Some($pat) = $x {}
87         };
88     }
89
90     // shouldn't be linted
91     if_let_in_macro!(_, Some(42));
92 }
93
94 // Methods that are unstable const should not be suggested within a const context, see issue #5697.
95 // However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
96 // so the following should be linted.
97 const fn issue6067() {
98     if Ok::<i32, i32>(42).is_ok() {}
99
100     if Err::<i32, i32>(42).is_err() {}
101
102     while Ok::<i32, i32>(10).is_ok() {}
103
104     while Ok::<i32, i32>(10).is_err() {}
105
106     Ok::<i32, i32>(42).is_ok();
107
108     Err::<i32, i32>(42).is_err();
109 }