Skip to content

Commit 98e6f43

Browse files
committed
remove trailing return in trailing match expression
1 parent cad222f commit 98e6f43

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

crates/hir-ty/src/diagnostics/expr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,12 @@ impl ExprValidator {
272272
self.check_for_trailing_return(*else_branch, body);
273273
}
274274
}
275+
Expr::Match { arms, .. } => {
276+
for arm in arms.iter() {
277+
let MatchArm { expr, .. } = arm;
278+
self.check_for_trailing_return(*expr, body);
279+
}
280+
}
275281
Expr::Return { .. } => {
276282
self.diagnostics.push(BodyValidationDiagnostic::RemoveTrailingReturn {
277283
return_expr: body_expr,

crates/hir/src/diagnostics.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use cfg::{CfgExpr, CfgOptions};
1111
use either::Either;
1212
use hir_def::{body::SyntheticSyntax, hir::ExprOrPatId, path::ModPath, AssocItemId, DefWithBodyId};
1313
use hir_expand::{name::Name, HirFileId, InFile};
14-
use syntax::{ast, AstPtr, SyntaxError, SyntaxNodePtr, TextRange};
14+
use syntax::{ast, AstNode, AstPtr, SyntaxError, SyntaxNodePtr, TextRange};
1515

1616
use crate::{AssocItem, Field, Local, MacroKind, Trait, Type};
1717

@@ -459,13 +459,16 @@ impl AnyDiagnostic {
459459
}
460460
BodyValidationDiagnostic::RemoveTrailingReturn { return_expr } => {
461461
if let Ok(source_ptr) = source_map.expr_syntax(return_expr) {
462-
return Some(
463-
RemoveTrailingReturn {
464-
file_id: source_ptr.file_id,
465-
return_expr: source_ptr.value,
466-
}
467-
.into(),
468-
);
462+
// Filters out desugared return expressions (e.g. desugared try operators).
463+
if ast::ReturnExpr::can_cast(source_ptr.value.kind()) {
464+
return Some(
465+
RemoveTrailingReturn {
466+
file_id: source_ptr.file_id,
467+
return_expr: source_ptr.value,
468+
}
469+
.into(),
470+
);
471+
}
469472
}
470473
}
471474
BodyValidationDiagnostic::RemoveUnnecessaryElse { if_expr } => {

crates/ide-diagnostics/src/handlers/remove_trailing_return.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ fn foo(x: usize) -> u8 {
147147
);
148148
}
149149

150+
#[test]
151+
fn remove_trailing_return_in_match() {
152+
check_diagnostics(
153+
r#"
154+
fn foo<T, E>(x: Result<T, E>) -> u8 {
155+
match x {
156+
Ok(_) => return 1,
157+
//^^^^^^^^ 💡 weak: replace return <expr>; with <expr>
158+
Err(_) => return 0,
159+
} //^^^^^^^^ 💡 weak: replace return <expr>; with <expr>
160+
}
161+
"#,
162+
);
163+
}
164+
150165
#[test]
151166
fn no_diagnostic_if_no_return_keyword() {
152167
check_diagnostics(
@@ -316,6 +331,46 @@ fn foo(x: usize) -> u8 {
316331
0
317332
}
318333
}
334+
"#,
335+
);
336+
}
337+
338+
#[test]
339+
fn replace_in_match() {
340+
check_fix(
341+
r#"
342+
fn foo<T, E>(x: Result<T, E>) -> u8 {
343+
match x {
344+
Ok(_) => return$0 1,
345+
Err(_) => 0,
346+
}
347+
}
348+
"#,
349+
r#"
350+
fn foo<T, E>(x: Result<T, E>) -> u8 {
351+
match x {
352+
Ok(_) => 1,
353+
Err(_) => 0,
354+
}
355+
}
356+
"#,
357+
);
358+
check_fix(
359+
r#"
360+
fn foo<T, E>(x: Result<T, E>) -> u8 {
361+
match x {
362+
Ok(_) => 1,
363+
Err(_) => return$0 0,
364+
}
365+
}
366+
"#,
367+
r#"
368+
fn foo<T, E>(x: Result<T, E>) -> u8 {
369+
match x {
370+
Ok(_) => 1,
371+
Err(_) => 0,
372+
}
373+
}
319374
"#,
320375
);
321376
}

0 commit comments

Comments
 (0)