Skip to content

Commit 8ddc1c8

Browse files
committed
Refine "remove semicolon" suggestion in trait selection
Don't suggest it if the last statement doesn't have a semicolon Fixes #81098 See also #54771 for why this suggestion was added
1 parent a8f7075 commit 8ddc1c8

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -888,8 +888,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
888888
// no return, suggest removal of semicolon on last statement.
889889
// Once that is added, close #54771.
890890
if let Some(ref stmt) = blk.stmts.last() {
891-
let sp = self.tcx.sess.source_map().end_point(stmt.span);
892-
err.span_label(sp, "consider removing this semicolon");
891+
if let hir::StmtKind::Semi(_) = stmt.kind {
892+
let sp = self.tcx.sess.source_map().end_point(stmt.span);
893+
err.span_label(sp, "consider removing this semicolon");
894+
}
893895
}
894896
}
895897
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Don't suggest removing a semicolon if the last statement isn't an expression with semicolon
2+
// (#81098)
3+
fn wat() -> impl core::fmt::Display { //~ ERROR: `()` doesn't implement `std::fmt::Display`
4+
fn why() {}
5+
}
6+
7+
// Do it if the last statement is an expression with semicolon
8+
// (#54771)
9+
fn ok() -> impl core::fmt::Display { //~ ERROR: `()` doesn't implement `std::fmt::Display`
10+
1;
11+
}
12+
13+
fn main() {}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0277]: `()` doesn't implement `std::fmt::Display`
2+
--> $DIR/issue-81098.rs:3:13
3+
|
4+
LL | fn wat() -> impl core::fmt::Display {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^ `()` cannot be formatted with the default formatter
6+
|
7+
= help: the trait `std::fmt::Display` is not implemented for `()`
8+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
9+
10+
error[E0277]: `()` doesn't implement `std::fmt::Display`
11+
--> $DIR/issue-81098.rs:9:12
12+
|
13+
LL | fn ok() -> impl core::fmt::Display {
14+
| ^^^^^^^^^^^^^^^^^^^^^^^ `()` cannot be formatted with the default formatter
15+
LL | 1;
16+
| - consider removing this semicolon
17+
|
18+
= help: the trait `std::fmt::Display` is not implemented for `()`
19+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)