File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed
ide-diagnostics/src/handlers Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -266,6 +266,12 @@ impl ExprValidator {
266266 self . check_for_trailing_return ( last_stmt, body) ;
267267 }
268268 }
269+ Expr :: If { then_branch, else_branch, .. } => {
270+ self . check_for_trailing_return ( * then_branch, body) ;
271+ if let Some ( else_branch) = else_branch {
272+ self . check_for_trailing_return ( * else_branch, body) ;
273+ }
274+ }
269275 Expr :: Return { .. } => {
270276 self . diagnostics . push ( BodyValidationDiagnostic :: RemoveTrailingReturn {
271277 return_expr : body_expr,
Original file line number Diff line number Diff line change @@ -131,6 +131,22 @@ fn foo() -> u8 {
131131 ) ;
132132 }
133133
134+ #[ test]
135+ fn remove_trailing_return_in_if ( ) {
136+ check_diagnostics (
137+ r#"
138+ fn foo(x: usize) -> u8 {
139+ if x > 0 {
140+ return 1;
141+ //^^^^^^^^^ 💡 weak: replace return <expr>; with <expr>
142+ } else {
143+ return 0;
144+ } //^^^^^^^^^ 💡 weak: replace return <expr>; with <expr>
145+ }
146+ "# ,
147+ ) ;
148+ }
149+
134150 #[ test]
135151 fn no_diagnostic_if_no_return_keyword ( ) {
136152 check_diagnostics (
@@ -256,6 +272,50 @@ fn foo() -> u8 {
256272 };
257273 bar()
258274}
275+ "# ,
276+ ) ;
277+ }
278+
279+ #[ test]
280+ fn replace_in_if ( ) {
281+ check_fix (
282+ r#"
283+ fn foo(x: usize) -> u8 {
284+ if x > 0 {
285+ return$0 1;
286+ } else {
287+ 0
288+ }
289+ }
290+ "# ,
291+ r#"
292+ fn foo(x: usize) -> u8 {
293+ if x > 0 {
294+ 1
295+ } else {
296+ 0
297+ }
298+ }
299+ "# ,
300+ ) ;
301+ check_fix (
302+ r#"
303+ fn foo(x: usize) -> u8 {
304+ if x > 0 {
305+ 1
306+ } else {
307+ return$0 0;
308+ }
309+ }
310+ "# ,
311+ r#"
312+ fn foo(x: usize) -> u8 {
313+ if x > 0 {
314+ 1
315+ } else {
316+ 0
317+ }
318+ }
259319"# ,
260320 ) ;
261321 }
You can’t perform that action at this time.
0 commit comments