]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/redundant_pattern_matching_result.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / redundant_pattern_matching_result.rs
CommitLineData
f20569fa
XL
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
13fn 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
68fn gen_res() -> Result<(), ()> {
69 Ok(())
70}
71
72macro_rules! m {
73 () => {
74 Some(42u32)
75 };
76}
77
78fn 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
95fn 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.
109const 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}