]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/redundant_pattern_matching_result.rs
New upstream version 1.72.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / redundant_pattern_matching_result.rs
CommitLineData
49aad941 1//@run-rustfix
f20569fa
XL
2#![warn(clippy::all)]
3#![warn(clippy::redundant_pattern_matching)]
2b03887a 4#![allow(deprecated, unused_must_use)]
f20569fa 5#![allow(
2b03887a 6 clippy::if_same_then_else,
f20569fa 7 clippy::match_like_matches_macro,
2b03887a 8 clippy::needless_bool,
fe692bf9 9 clippy::needless_if,
2b03887a
FG
10 clippy::uninlined_format_args,
11 clippy::unnecessary_wraps
f20569fa
XL
12)]
13
14fn 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();
49aad941 59 issue10726();
fe692bf9 60 issue10803();
f20569fa
XL
61
62 let _ = if let Ok(_) = gen_res() {
63 1
64 } else if let Err(_) = gen_res() {
65 2
66 } else {
67 3
68 };
69}
70
71fn gen_res() -> Result<(), ()> {
72 Ok(())
73}
74
75macro_rules! m {
76 () => {
77 Some(42u32)
78 };
79}
80
81fn issue5504() {
82 fn result_opt() -> Result<Option<i32>, i32> {
83 Err(42)
84 }
85
86 fn try_result_opt() -> Result<i32, i32> {
87 while let Some(_) = r#try!(result_opt()) {}
88 if let Some(_) = r#try!(result_opt()) {}
89 Ok(42)
90 }
91
92 try_result_opt();
93
94 if let Some(_) = m!() {}
95 while let Some(_) = m!() {}
96}
97
98fn issue6065() {
99 macro_rules! if_let_in_macro {
100 ($pat:pat, $x:expr) => {
101 if let Some($pat) = $x {}
102 };
103 }
104
105 // shouldn't be linted
106 if_let_in_macro!(_, Some(42));
107}
108
109// Methods that are unstable const should not be suggested within a const context, see issue #5697.
110// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
111// so the following should be linted.
112const fn issue6067() {
113 if let Ok(_) = Ok::<i32, i32>(42) {}
114
115 if let Err(_) = Err::<i32, i32>(42) {}
116
117 while let Ok(_) = Ok::<i32, i32>(10) {}
118
119 while let Err(_) = Ok::<i32, i32>(10) {}
120
121 match Ok::<i32, i32>(42) {
122 Ok(_) => true,
123 Err(_) => false,
124 };
125
126 match Err::<i32, i32>(42) {
127 Ok(_) => false,
128 Err(_) => true,
129 };
130}
49aad941
FG
131
132fn issue10726() {
133 // This is optional, but it makes the examples easier
134 let x: Result<i32, i32> = Ok(42);
135
136 match x {
137 Ok(_) => true,
138 _ => false,
139 };
140
141 match x {
142 Ok(_) => false,
143 _ => true,
144 };
145
146 match x {
147 Err(_) => true,
148 _ => false,
149 };
150
151 match x {
152 Err(_) => false,
153 _ => true,
154 };
155
156 // Don't lint
157 match x {
158 Err(16) => false,
159 _ => true,
160 };
161
162 // Don't lint
163 match x {
164 Ok(16) => false,
165 _ => true,
166 };
167}
fe692bf9
FG
168
169fn issue10803() {
170 let x: Result<i32, i32> = Ok(42);
171
172 let _ = matches!(x, Ok(_));
173
174 let _ = matches!(x, Err(_));
175
176 // Don't lint
177 let _ = matches!(x, Ok(16));
178
179 // Don't lint
180 let _ = matches!(x, Err(16));
181}