Skip to content

Do not consider using a semicolon when a return would work better #82425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
@@ -393,12 +393,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
| ExprKind::If(..)
| ExprKind::Match(..)
| ExprKind::Block(..) => {
err.span_suggestion(
cause_span.shrink_to_hi(),
"try adding a semicolon",
";".to_string(),
Applicability::MachineApplicable,
);
self.suggest_return_or_semicolon(err, expression, cause_span);
}
_ => (),
}
@@ -475,4 +470,31 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.sess.parse_sess.expr_parentheses_needed(err, *sp, None);
}
}

pub(in super::super) fn suggest_return_or_semicolon(
&self,
err: &mut DiagnosticBuilder<'_>,
expression: &'tcx hir::Expr<'tcx>,
cause_span: Span,
) {
if let Some(ref ret_coercion) = self.ret_coercion {
let ret_ty = ret_coercion.borrow().expected_ty();
let return_expr_ty = self.check_expr_with_hint(expression, ret_ty.clone());
if self.can_coerce(return_expr_ty, ret_ty) {
err.span_suggestion_short(
cause_span.shrink_to_lo(),
"try adding return",
"return ".to_string(),
Applicability::MachineApplicable,
);
return;
}
}
err.span_suggestion(
cause_span.shrink_to_hi(),
"try adding a semicolon",
";".to_string(),
Applicability::MachineApplicable,
);
}
}
14 changes: 14 additions & 0 deletions src/test/ui/typeck/issue-52284.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct Alef();
struct Bet();
fn gimel() -> Alef {
if true {
Alef() //~ERROR
}
if true {
Bet() //~ERROR
}
Alef()
}
fn main() {
gimel();
}
39 changes: 39 additions & 0 deletions src/test/ui/typeck/issue-52284.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
error[E0308]: mismatched types
--> $DIR/issue-52284.rs:5:5
|
LL | / if true {
LL | | Alef()
| | ^^^^^^ expected `()`, found struct `Alef`
LL | | }
| |___- expected this to be `()`
|
help: try adding return
|
LL | return Alef()
| ^^^^^^
help: consider using a semicolon here
|
LL | };
| ^

error[E0308]: mismatched types
--> $DIR/issue-52284.rs:8:5
|
LL | / if true {
LL | | Bet()
| | ^^^^^ expected `()`, found struct `Bet`
LL | | }
| |___- expected this to be `()`
|
help: try adding a semicolon
|
LL | Bet();
| ^
help: consider using a semicolon here
|
LL | };
| ^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.