Skip to content

Commit d68570c

Browse files
authored
Rollup merge of #81407 - osa1:issue81098, r=estebank
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
2 parents 24a1081 + 8ddc1c8 commit d68570c

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
@@ -899,8 +899,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
899899
// no return, suggest removal of semicolon on last statement.
900900
// Once that is added, close #54771.
901901
if let Some(ref stmt) = blk.stmts.last() {
902-
let sp = self.tcx.sess.source_map().end_point(stmt.span);
903-
err.span_label(sp, "consider removing this semicolon");
902+
if let hir::StmtKind::Semi(_) = stmt.kind {
903+
let sp = self.tcx.sess.source_map().end_point(stmt.span);
904+
err.span_label(sp, "consider removing this semicolon");
905+
}
904906
}
905907
}
906908
}
+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)