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

Refine "remove semicolon" suggestion in trait selection #81407

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
// no return, suggest removal of semicolon on last statement.
// Once that is added, close #54771.
if let Some(ref stmt) = blk.stmts.last() {
let sp = self.tcx.sess.source_map().end_point(stmt.span);
err.span_label(sp, "consider removing this semicolon");
if let hir::StmtKind::Semi(_) = stmt.kind {
let sp = self.tcx.sess.source_map().end_point(stmt.span);
err.span_label(sp, "consider removing this semicolon");
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/suggestions/issue-81098.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Don't suggest removing a semicolon if the last statement isn't an expression with semicolon
// (#81098)
fn wat() -> impl core::fmt::Display { //~ ERROR: `()` doesn't implement `std::fmt::Display`
fn why() {}
}

// Do it if the last statement is an expression with semicolon
// (#54771)
fn ok() -> impl core::fmt::Display { //~ ERROR: `()` doesn't implement `std::fmt::Display`
1;
}

fn main() {}
23 changes: 23 additions & 0 deletions src/test/ui/suggestions/issue-81098.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0277]: `()` doesn't implement `std::fmt::Display`
--> $DIR/issue-81098.rs:3:13
|
LL | fn wat() -> impl core::fmt::Display {
| ^^^^^^^^^^^^^^^^^^^^^^^ `()` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `()`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead

error[E0277]: `()` doesn't implement `std::fmt::Display`
--> $DIR/issue-81098.rs:9:12
|
LL | fn ok() -> impl core::fmt::Display {
| ^^^^^^^^^^^^^^^^^^^^^^^ `()` cannot be formatted with the default formatter
LL | 1;
| - consider removing this semicolon
|
= help: the trait `std::fmt::Display` is not implemented for `()`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead

error: aborting due to 2 previous errors

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