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