mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Auto merge of #16574 - davidsemakula:needless-return-fix, r=Veykril
fix: Fix "needless return" diagnostic for trailing item declarations Fixes #16566
This commit is contained in:
commit
b9b0d29b8e
@ -1113,7 +1113,7 @@ impl ExprCollector<'_> {
|
|||||||
statements.push(Statement::Expr { expr, has_semi });
|
statements.push(Statement::Expr { expr, has_semi });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast::Stmt::Item(_item) => (),
|
ast::Stmt::Item(_item) => statements.push(Statement::Item),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -628,6 +628,7 @@ impl Printer<'_> {
|
|||||||
}
|
}
|
||||||
wln!(self);
|
wln!(self);
|
||||||
}
|
}
|
||||||
|
Statement::Item => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,6 +197,7 @@ fn compute_block_scopes(
|
|||||||
Statement::Expr { expr, .. } => {
|
Statement::Expr { expr, .. } => {
|
||||||
compute_expr_scopes(*expr, body, scopes, scope);
|
compute_expr_scopes(*expr, body, scopes, scope);
|
||||||
}
|
}
|
||||||
|
Statement::Item => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(expr) = tail {
|
if let Some(expr) = tail {
|
||||||
|
@ -352,6 +352,9 @@ pub enum Statement {
|
|||||||
expr: ExprId,
|
expr: ExprId,
|
||||||
has_semi: bool,
|
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 {
|
impl Expr {
|
||||||
@ -385,6 +388,7 @@ impl Expr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Statement::Expr { expr: expression, .. } => f(*expression),
|
Statement::Expr { expr: expression, .. } => f(*expression),
|
||||||
|
Statement::Item => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let &Some(expr) = tail {
|
if let &Some(expr) = tail {
|
||||||
|
@ -485,6 +485,7 @@ impl InferenceContext<'_> {
|
|||||||
Statement::Expr { expr, has_semi: _ } => {
|
Statement::Expr { expr, has_semi: _ } => {
|
||||||
self.consume_expr(*expr);
|
self.consume_expr(*expr);
|
||||||
}
|
}
|
||||||
|
Statement::Item => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(tail) = tail {
|
if let Some(tail) = tail {
|
||||||
|
@ -1389,6 +1389,7 @@ impl InferenceContext<'_> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Statement::Item => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +65,7 @@ impl InferenceContext<'_> {
|
|||||||
Statement::Expr { expr, has_semi: _ } => {
|
Statement::Expr { expr, has_semi: _ } => {
|
||||||
self.infer_mut_expr(*expr, Mutability::Not);
|
self.infer_mut_expr(*expr, Mutability::Not);
|
||||||
}
|
}
|
||||||
|
Statement::Item => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(tail) = tail {
|
if let Some(tail) = tail {
|
||||||
|
@ -1781,6 +1781,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
|
|||||||
self.push_fake_read(c, p, expr.into());
|
self.push_fake_read(c, p, expr.into());
|
||||||
current = scope2.pop_and_drop(self, c, expr.into());
|
current = scope2.pop_and_drop(self, c, expr.into());
|
||||||
}
|
}
|
||||||
|
hir_def::hir::Statement::Item => (),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(tail) = tail {
|
if let Some(tail) = tail {
|
||||||
|
@ -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]
|
#[test]
|
||||||
fn replace_with_expr() {
|
fn replace_with_expr() {
|
||||||
check_fix(
|
check_fix(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user