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