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