Skip to content

Commit b9b0d29

Browse files
committed
Auto merge of #16574 - davidsemakula:needless-return-fix, r=Veykril
fix: Fix "needless return" diagnostic for trailing item declarations Fixes #16566
2 parents b30b77d + 9ae0f92 commit b9b0d29

File tree

9 files changed

+23
-1
lines changed

9 files changed

+23
-1
lines changed

Diff for: crates/hir-def/src/body/lower.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ impl ExprCollector<'_> {
11131113
statements.push(Statement::Expr { expr, has_semi });
11141114
}
11151115
}
1116-
ast::Stmt::Item(_item) => (),
1116+
ast::Stmt::Item(_item) => statements.push(Statement::Item),
11171117
}
11181118
}
11191119

Diff for: crates/hir-def/src/body/pretty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ impl Printer<'_> {
628628
}
629629
wln!(self);
630630
}
631+
Statement::Item => (),
631632
}
632633
}
633634

Diff for: crates/hir-def/src/body/scope.rs

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ fn compute_block_scopes(
197197
Statement::Expr { expr, .. } => {
198198
compute_expr_scopes(*expr, body, scopes, scope);
199199
}
200+
Statement::Item => (),
200201
}
201202
}
202203
if let Some(expr) = tail {

Diff for: crates/hir-def/src/hir.rs

+4
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ pub enum Statement {
352352
expr: ExprId,
353353
has_semi: bool,
354354
},
355+
// At the moment, we only use this to figure out if a return expression
356+
// is really the last statement of a block. See #16566
357+
Item,
355358
}
356359

357360
impl Expr {
@@ -385,6 +388,7 @@ impl Expr {
385388
}
386389
}
387390
Statement::Expr { expr: expression, .. } => f(*expression),
391+
Statement::Item => (),
388392
}
389393
}
390394
if let &Some(expr) = tail {

Diff for: crates/hir-ty/src/infer/closure.rs

+1
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ impl InferenceContext<'_> {
485485
Statement::Expr { expr, has_semi: _ } => {
486486
self.consume_expr(*expr);
487487
}
488+
Statement::Item => (),
488489
}
489490
}
490491
if let Some(tail) = tail {

Diff for: crates/hir-ty/src/infer/expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,7 @@ impl InferenceContext<'_> {
13891389
);
13901390
}
13911391
}
1392+
Statement::Item => (),
13921393
}
13931394
}
13941395

Diff for: crates/hir-ty/src/infer/mutability.rs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl InferenceContext<'_> {
6565
Statement::Expr { expr, has_semi: _ } => {
6666
self.infer_mut_expr(*expr, Mutability::Not);
6767
}
68+
Statement::Item => (),
6869
}
6970
}
7071
if let Some(tail) = tail {

Diff for: crates/hir-ty/src/mir/lower.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
17811781
self.push_fake_read(c, p, expr.into());
17821782
current = scope2.pop_and_drop(self, c, expr.into());
17831783
}
1784+
hir_def::hir::Statement::Item => (),
17841785
}
17851786
}
17861787
if let Some(tail) = tail {

Diff for: crates/ide-diagnostics/src/handlers/remove_trailing_return.rs

+12
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,18 @@ fn foo() -> u8 {
182182
);
183183
}
184184

185+
#[test]
186+
fn no_diagnostic_if_not_last_statement2() {
187+
check_diagnostics(
188+
r#"
189+
fn foo() -> u8 {
190+
return 2;
191+
fn bar() {}
192+
}
193+
"#,
194+
);
195+
}
196+
185197
#[test]
186198
fn replace_with_expr() {
187199
check_fix(

0 commit comments

Comments
 (0)