File tree 3 files changed +71
-1
lines changed 3 files changed +71
-1
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,31 @@ fn func() -> Option<i32> {
104
104
Some(0)
105
105
}
106
106
107
+ fn result_func(x: Result<i32, &str>) -> Result<i32, &str> {
108
+ let _ = x?;
109
+
110
+ x?;
111
+
112
+ // No warning
113
+ let y = if let Ok(x) = x {
114
+ x
115
+ } else {
116
+ return Err("some error");
117
+ };
118
+
119
+ let _ = match x {
120
+ Ok(x) => (),
121
+ _ => return x,
122
+ };
123
+
124
+ let _ = match x {
125
+ Ok(x) => (),
126
+ _ => return Err("some error"),
127
+ };
128
+
129
+ Ok(y)
130
+ }
131
+
107
132
fn main() {
108
133
some_func(Some(42));
109
134
some_func(None);
@@ -123,4 +148,6 @@ fn main() {
123
148
returns_something_similar_to_option(so);
124
149
125
150
func();
151
+
152
+ result_func(Ok(42));
126
153
}
Original file line number Diff line number Diff line change @@ -134,6 +134,33 @@ fn func() -> Option<i32> {
134
134
Some ( 0 )
135
135
}
136
136
137
+ fn result_func ( x : Result < i32 , & str > ) -> Result < i32 , & str > {
138
+ let _ = if let Ok ( x) = x { x } else { return x } ;
139
+
140
+ if x. is_err ( ) {
141
+ return x;
142
+ }
143
+
144
+ // No warning
145
+ let y = if let Ok ( x) = x {
146
+ x
147
+ } else {
148
+ return Err ( "some error" ) ;
149
+ } ;
150
+
151
+ let _ = match x {
152
+ Ok ( x) => ( ) ,
153
+ _ => return x,
154
+ } ;
155
+
156
+ let _ = match x {
157
+ Ok ( x) => ( ) ,
158
+ _ => return Err ( "some error" ) ,
159
+ } ;
160
+
161
+ Ok ( y)
162
+ }
163
+
137
164
fn main ( ) {
138
165
some_func ( Some ( 42 ) ) ;
139
166
some_func ( None ) ;
@@ -153,4 +180,6 @@ fn main() {
153
180
returns_something_similar_to_option ( so) ;
154
181
155
182
func ( ) ;
183
+
184
+ result_func ( Ok ( 42 ) ) ;
156
185
}
Original file line number Diff line number Diff line change @@ -100,5 +100,19 @@ LL | | return None;
100
100
LL | | }
101
101
| |_____^ help: replace it with: `f()?;`
102
102
103
- error: aborting due to 11 previous errors
103
+ error: this if-let-else may be rewritten with the `?` operator
104
+ --> $DIR/question_mark.rs:138:13
105
+ |
106
+ LL | let _ = if let Ok(x) = x { x } else { return x };
107
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace it with: `x?`
108
+
109
+ error: this block may be rewritten with the `?` operator
110
+ --> $DIR/question_mark.rs:140:5
111
+ |
112
+ LL | / if x.is_err() {
113
+ LL | | return x;
114
+ LL | | }
115
+ | |_____^ help: replace it with: `x?;`
116
+
117
+ error: aborting due to 13 previous errors
104
118
You can’t perform that action at this time.
0 commit comments