This repository was archived by the owner on May 28, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Expand file tree Collapse file tree 3 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -104,6 +104,21 @@ fn func() -> Option<i32> {
104104 Some(0)
105105}
106106
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+ Ok(y)
120+ }
121+
107122fn main() {
108123 some_func(Some(42));
109124 some_func(None);
@@ -123,4 +138,6 @@ fn main() {
123138 returns_something_similar_to_option(so);
124139
125140 func();
141+
142+ let _ = result_func(Ok(42));
126143}
Original file line number Diff line number Diff line change @@ -134,6 +134,23 @@ fn func() -> Option<i32> {
134134 Some ( 0 )
135135}
136136
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+ Ok ( y)
152+ }
153+
137154fn main ( ) {
138155 some_func ( Some ( 42 ) ) ;
139156 some_func ( None ) ;
@@ -153,4 +170,6 @@ fn main() {
153170 returns_something_similar_to_option ( so) ;
154171
155172 func ( ) ;
173+
174+ let _ = result_func ( Ok ( 42 ) ) ;
156175}
Original file line number Diff line number Diff line change @@ -100,5 +100,19 @@ LL | | return None;
100100LL | | }
101101 | |_____^ help: replace it with: `f()?;`
102102
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
104118
You can’t perform that action at this time.
0 commit comments