Skip to content

Commit

Permalink
fix "needless return" for trailing item declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsemakula committed Feb 15, 2024
1 parent db277c7 commit 9ae0f92
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ impl ExprCollector<'_> {
statements.push(Statement::Expr { expr, has_semi });
}
}
ast::Stmt::Item(_item) => (),
ast::Stmt::Item(_item) => statements.push(Statement::Item),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/hir-def/src/body/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ impl Printer<'_> {
}
wln!(self);
}
Statement::Item => (),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/hir-def/src/body/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ fn compute_block_scopes(
Statement::Expr { expr, .. } => {
compute_expr_scopes(*expr, body, scopes, scope);
}
Statement::Item => (),
}
}
if let Some(expr) = tail {
Expand Down
4 changes: 4 additions & 0 deletions crates/hir-def/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ pub enum Statement {
expr: ExprId,
has_semi: bool,
},
// At the moment, we only use this to figure out if a return expression
// is really the last statement of a block. See #16566
Item,
}

impl Expr {
Expand Down Expand Up @@ -385,6 +388,7 @@ impl Expr {
}
}
Statement::Expr { expr: expression, .. } => f(*expression),
Statement::Item => (),
}
}
if let &Some(expr) = tail {
Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/src/infer/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ impl InferenceContext<'_> {
Statement::Expr { expr, has_semi: _ } => {
self.consume_expr(*expr);
}
Statement::Item => (),
}
}
if let Some(tail) = tail {
Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/src/infer/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,7 @@ impl InferenceContext<'_> {
);
}
}
Statement::Item => (),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/src/infer/mutability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl InferenceContext<'_> {
Statement::Expr { expr, has_semi: _ } => {
self.infer_mut_expr(*expr, Mutability::Not);
}
Statement::Item => (),
}
}
if let Some(tail) = tail {
Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/src/mir/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
self.push_fake_read(c, p, expr.into());
current = scope2.pop_and_drop(self, c, expr.into());
}
hir_def::hir::Statement::Item => (),
}
}
if let Some(tail) = tail {
Expand Down
12 changes: 12 additions & 0 deletions crates/ide-diagnostics/src/handlers/remove_trailing_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ fn foo() -> u8 {
);
}

#[test]
fn no_diagnostic_if_not_last_statement2() {
check_diagnostics(
r#"
fn foo() -> u8 {
return 2;
fn bar() {}
}
"#,
);
}

#[test]
fn replace_with_expr() {
check_fix(
Expand Down

0 comments on commit 9ae0f92

Please sign in to comment.