Skip to content
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

Suggest removing superfluous semicolon when statements used as expression #121153

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
renaming test cases
  • Loading branch information
chenyukang committed Feb 29, 2024
commit e2ce5d74a5f87f9fb6baadd191288025ed974296
39 changes: 0 additions & 39 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1791,45 +1791,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}

/// A common error is to add an extra semicolon:
///
/// ```compile_fail,E0308
/// fn foo() -> usize {
/// 22;
/// }
/// ```
///
/// This routine checks if the final statement in a block is an
/// expression with an explicit semicolon whose type is compatible
/// with `expected_ty`. If so, it suggests removing the semicolon.
pub(crate) fn consider_removing_semicolon(
&self,
blk: &'tcx hir::Block<'tcx>,
expected_ty: Ty<'tcx>,
err: &mut Diag<'_>,
) -> bool {
if let Some((span_semi, boxed)) = self.err_ctxt().could_remove_semicolon(blk, expected_ty) {
if let StatementAsExpression::NeedsBoxing = boxed {
err.span_suggestion_verbose(
span_semi,
"consider removing this semicolon and boxing the expression",
"",
Applicability::HasPlaceholders,
);
} else {
err.span_suggestion_short(
span_semi,
"remove this semicolon to return this value",
"",
Applicability::MachineApplicable,
);
}
true
} else {
false
}
}

pub(crate) fn is_field_suggestable(
&self,
field: &ty::FieldDef,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,7 +1976,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
self.suggest_function_pointers(cause, span, &exp_found, diag);
self.suggest_for_statments_as_exp(cause, &exp_found, diag);
self.suggest_turning_stmt_into_expr(cause, &exp_found, diag);
}
}

Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_infer/src/infer/error_reporting/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}

pub(super) fn suggest_for_statments_as_exp(
pub(super) fn suggest_turning_stmt_into_expr(
&self,
cause: &ObligationCause<'tcx>,
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
diag: &mut Diagnostic,
diag: &mut Diag<'_>,
) {
let ty::error::ExpectedFound { expected, found } = exp_found;
if !found.peel_refs().is_unit() {
Expand Down Expand Up @@ -365,18 +365,18 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&self,
blk: &'tcx hir::Block<'tcx>,
expected_ty: Ty<'tcx>,
err: &mut Diagnostic,
diag: &mut Diag<'_>,
) -> bool {
if let Some((span_semi, boxed)) = self.could_remove_semicolon(blk, expected_ty) {
if let StatementAsExpression::NeedsBoxing = boxed {
err.span_suggestion_verbose(
diag.span_suggestion_verbose(
span_semi,
"consider removing this semicolon and boxing the expression",
"",
Applicability::HasPlaceholders,
);
} else {
err.span_suggestion_short(
diag.span_suggestion_short(
span_semi,
"remove this semicolon to return this value",
"",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/issue-105431-stmts-as-exp.rs:11:5
--> $DIR/stmts-as-exp-105431.rs:11:5
|
LL | fn test_if() -> i32 {
| --- expected `i32` because of return type
Expand All @@ -19,7 +19,7 @@ LL + 4
|

error[E0308]: mismatched types
--> $DIR/issue-105431-stmts-as-exp.rs:15:13
--> $DIR/stmts-as-exp-105431.rs:15:13
|
LL | if true {
| _____________^
Expand All @@ -30,7 +30,7 @@ LL | | }
| |_____^ expected `i32`, found `()`

error[E0308]: mismatched types
--> $DIR/issue-105431-stmts-as-exp.rs:19:10
--> $DIR/stmts-as-exp-105431.rs:19:10
|
LL | else {
| __________^
Expand All @@ -40,7 +40,7 @@ LL | | }
| |_____^ expected `i32`, found `()`

error[E0308]: mismatched types
--> $DIR/issue-105431-stmts-as-exp.rs:30:5
--> $DIR/stmts-as-exp-105431.rs:30:5
|
LL | fn test_match() -> i32 {
| --- expected `i32` because of return type
Expand All @@ -60,7 +60,7 @@ LL + _ => { 2 }
|

error[E0308]: mismatched types
--> $DIR/issue-105431-stmts-as-exp.rs:36:14
--> $DIR/stmts-as-exp-105431.rs:36:14
|
LL | 1 => { 1; }
| ^^^-^^
Expand All @@ -69,7 +69,7 @@ LL | 1 => { 1; }
| expected `i32`, found `()`

error[E0308]: mismatched types
--> $DIR/issue-105431-stmts-as-exp.rs:37:14
--> $DIR/stmts-as-exp-105431.rs:37:14
|
LL | _ => { 2; }
| ^^^-^^
Expand All @@ -78,7 +78,7 @@ LL | _ => { 2; }
| expected `i32`, found `()`

error[E0308]: `match` arms have incompatible types
--> $DIR/issue-105431-stmts-as-exp.rs:45:16
--> $DIR/stmts-as-exp-105431.rs:45:16
|
LL | let res = match v {
| _______________-
Expand All @@ -93,7 +93,7 @@ LL | | };
| |_____- `match` arms have incompatible types

error[E0308]: mismatched types
--> $DIR/issue-105431-stmts-as-exp.rs:59:5
--> $DIR/stmts-as-exp-105431.rs:59:5
|
LL | fn test_if_match_mixed() -> i32 {
| --- expected `i32` because of return type
Expand All @@ -113,7 +113,7 @@ LL + }
|

error[E0308]: mismatched types
--> $DIR/issue-105431-stmts-as-exp.rs:72:5
--> $DIR/stmts-as-exp-105431.rs:72:5
|
LL | fn test_if_match_mixed_failed() -> i32 {
| --- expected `i32` because of return type
Expand Down
Loading