]> git.lizzy.rs Git - rust.git/blob - tests/ui/unnecessary_wraps.rs
Add version = "Two" to rustfmt.toml
[rust.git] / tests / ui / unnecessary_wraps.rs
1 #![warn(clippy::unnecessary_wraps)]
2 #![allow(clippy::no_effect)]
3 #![allow(clippy::needless_return)]
4 #![allow(clippy::if_same_then_else)]
5 #![allow(dead_code)]
6
7 // should be linted
8 fn func1(a: bool, b: bool) -> Option<i32> {
9     if a && b {
10         return Some(42);
11     }
12     if a {
13         Some(-1);
14         Some(2)
15     } else {
16         return Some(1337);
17     }
18 }
19
20 // should be linted
21 fn func2(a: bool, b: bool) -> Option<i32> {
22     if a && b {
23         return Some(10);
24     }
25     if a { Some(20) } else { Some(30) }
26 }
27
28 // public fns should not be linted
29 pub fn func3(a: bool) -> Option<i32> {
30     if a { Some(1) } else { Some(1) }
31 }
32
33 // should not be linted
34 fn func4(a: bool) -> Option<i32> {
35     if a { Some(1) } else { None }
36 }
37
38 // should be linted
39 fn func5() -> Option<i32> {
40     Some(1)
41 }
42
43 // should not be linted
44 fn func6() -> Option<i32> {
45     None
46 }
47
48 // should be linted
49 fn func7() -> Result<i32, ()> {
50     Ok(1)
51 }
52
53 // should not be linted
54 fn func8(a: bool) -> Result<i32, ()> {
55     if a { Ok(1) } else { Err(()) }
56 }
57
58 // should not be linted
59 fn func9(a: bool) -> Result<i32, ()> {
60     Err(())
61 }
62
63 // should not be linted
64 fn func10() -> Option<()> {
65     unimplemented!()
66 }
67
68 struct A;
69
70 impl A {
71     // should not be linted
72     pub fn func11() -> Option<i32> {
73         Some(1)
74     }
75
76     // should be linted
77     fn func12() -> Option<i32> {
78         Some(1)
79     }
80 }
81
82 trait B {
83     // trait impls are not linted
84     fn func13() -> Option<i32> {
85         Some(1)
86     }
87 }
88
89 impl B for A {
90     // trait impls are not linted
91     fn func13() -> Option<i32> {
92         Some(0)
93     }
94 }
95
96 fn issue_6384(s: &str) -> Option<&str> {
97     Some(match s {
98         "a" => "A",
99         _ => return None,
100     })
101 }
102
103 // should be linted
104 fn issue_6640_1(a: bool, b: bool) -> Option<()> {
105     if a && b {
106         return Some(());
107     }
108     if a {
109         Some(());
110         Some(())
111     } else {
112         return Some(());
113     }
114 }
115
116 // should be linted
117 fn issue_6640_2(a: bool, b: bool) -> Result<(), i32> {
118     if a && b {
119         return Ok(());
120     }
121     if a {
122         Ok(())
123     } else {
124         return Ok(());
125     }
126 }
127
128 // should not be linted
129 fn issue_6640_3() -> Option<()> {
130     if true { Some(()) } else { None }
131 }
132
133 // should not be linted
134 fn issue_6640_4() -> Result<(), ()> {
135     if true { Ok(()) } else { Err(()) }
136 }
137
138 fn main() {
139     // method calls are not linted
140     func1(true, true);
141     func2(true, true);
142     issue_6640_1(true, true);
143     issue_6640_2(true, true);
144 }