Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--crates/hir-ty/src/diagnostics/expr.rs6
-rw-r--r--crates/ide-diagnostics/src/handlers/remove_trailing_return.rs60
2 files changed, 66 insertions, 0 deletions
diff --git a/crates/hir-ty/src/diagnostics/expr.rs b/crates/hir-ty/src/diagnostics/expr.rs
index af3ca3c082..50312af4f2 100644
--- a/crates/hir-ty/src/diagnostics/expr.rs
+++ b/crates/hir-ty/src/diagnostics/expr.rs
@@ -266,6 +266,12 @@ impl ExprValidator {
self.check_for_trailing_return(last_stmt, body);
}
}
+ Expr::If { then_branch, else_branch, .. } => {
+ self.check_for_trailing_return(*then_branch, body);
+ if let Some(else_branch) = else_branch {
+ self.check_for_trailing_return(*else_branch, body);
+ }
+ }
Expr::Return { .. } => {
self.diagnostics.push(BodyValidationDiagnostic::RemoveTrailingReturn {
return_expr: body_expr,
diff --git a/crates/ide-diagnostics/src/handlers/remove_trailing_return.rs b/crates/ide-diagnostics/src/handlers/remove_trailing_return.rs
index 6cb5911096..ceef9da16c 100644
--- a/crates/ide-diagnostics/src/handlers/remove_trailing_return.rs
+++ b/crates/ide-diagnostics/src/handlers/remove_trailing_return.rs
@@ -132,6 +132,22 @@ fn foo() -> u8 {
}
#[test]
+ fn remove_trailing_return_in_if() {
+ check_diagnostics(
+ r#"
+fn foo(x: usize) -> u8 {
+ if x > 0 {
+ return 1;
+ //^^^^^^^^^ 💡 weak: replace return <expr>; with <expr>
+ } else {
+ return 0;
+ } //^^^^^^^^^ 💡 weak: replace return <expr>; with <expr>
+}
+"#,
+ );
+ }
+
+ #[test]
fn no_diagnostic_if_no_return_keyword() {
check_diagnostics(
r#"
@@ -259,4 +275,48 @@ fn foo() -> u8 {
"#,
);
}
+
+ #[test]
+ fn replace_in_if() {
+ check_fix(
+ r#"
+fn foo(x: usize) -> u8 {
+ if x > 0 {
+ return$0 1;
+ } else {
+ 0
+ }
+}
+"#,
+ r#"
+fn foo(x: usize) -> u8 {
+ if x > 0 {
+ 1
+ } else {
+ 0
+ }
+}
+"#,
+ );
+ check_fix(
+ r#"
+fn foo(x: usize) -> u8 {
+ if x > 0 {
+ 1
+ } else {
+ return$0 0;
+ }
+}
+"#,
+ r#"
+fn foo(x: usize) -> u8 {
+ if x > 0 {
+ 1
+ } else {
+ 0
+ }
+}
+"#,
+ );
+ }
}