Skip to content

Commit ad0531a

Browse files
authored
Rollup merge of #126455 - surechen:fix_126222, r=estebank
For [E0308]: mismatched types, when expr is in an arm's body, not add semicolon ';' at the end of it. For [E0308]: mismatched types, when expr is in an arm's body, and it is the end expr without a semicolon of the block, not add semicolon ';' at the end of it. fixes #126222 <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r​? <reviewer name> -->
2 parents 7babf99 + e8b5ba1 commit ad0531a

File tree

4 files changed

+170
-9
lines changed

4 files changed

+170
-9
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+51-9
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ use rustc_middle::lint::in_external_macro;
2626
use rustc_middle::middle::stability::EvalResult;
2727
use rustc_middle::span_bug;
2828
use rustc_middle::ty::print::with_no_trimmed_paths;
29-
use rustc_middle::ty::{
30-
self, suggest_constraining_type_params, Article, Binder, IsSuggestable, Ty, TypeVisitableExt,
31-
Upcast,
32-
};
29+
use rustc_middle::ty::{self, suggest_constraining_type_params, Article, Binder};
30+
use rustc_middle::ty::{IsSuggestable, Ty, TyCtxt, TypeVisitableExt, Upcast};
3331
use rustc_session::errors::ExprParenthesesNeeded;
3432
use rustc_span::source_map::Spanned;
3533
use rustc_span::symbol::{sym, Ident};
@@ -1111,12 +1109,56 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11111109
self.tcx.hir().span_with_body(self.tcx.local_def_id_to_hir_id(fn_id)),
11121110
)
11131111
{
1114-
err.multipart_suggestion(
1112+
// When the expr is in a match arm's body, we shouldn't add semicolon ';' at the end.
1113+
// For example:
1114+
// fn mismatch_types() -> i32 {
1115+
// match 1 {
1116+
// x => dbg!(x),
1117+
// }
1118+
// todo!()
1119+
// }
1120+
// -------------^^^^^^^-
1121+
// Don't add semicolon `;` at the end of `dbg!(x)` expr
1122+
fn is_in_arm<'tcx>(expr: &'tcx hir::Expr<'tcx>, tcx: TyCtxt<'tcx>) -> bool {
1123+
for (_, node) in tcx.hir().parent_iter(expr.hir_id) {
1124+
match node {
1125+
hir::Node::Block(block) => {
1126+
if let Some(ret) = block.expr
1127+
&& ret.hir_id == expr.hir_id
1128+
{
1129+
continue;
1130+
}
1131+
}
1132+
hir::Node::Arm(arm) => {
1133+
if let hir::ExprKind::Block(block, _) = arm.body.kind
1134+
&& let Some(ret) = block.expr
1135+
&& ret.hir_id == expr.hir_id
1136+
{
1137+
return true;
1138+
}
1139+
}
1140+
hir::Node::Expr(e) if let hir::ExprKind::Block(block, _) = e.kind => {
1141+
if let Some(ret) = block.expr
1142+
&& ret.hir_id == expr.hir_id
1143+
{
1144+
continue;
1145+
}
1146+
}
1147+
_ => {
1148+
return false;
1149+
}
1150+
}
1151+
}
1152+
1153+
false
1154+
}
1155+
let mut suggs = vec![(span.shrink_to_lo(), "return ".to_string())];
1156+
if !is_in_arm(expr, self.tcx) {
1157+
suggs.push((span.shrink_to_hi(), ";".to_string()));
1158+
}
1159+
err.multipart_suggestion_verbose(
11151160
"you might have meant to return this value",
1116-
vec![
1117-
(span.shrink_to_lo(), "return ".to_string()),
1118-
(span.shrink_to_hi(), ";".to_string()),
1119-
],
1161+
suggs,
11201162
Applicability::MaybeIncorrect,
11211163
);
11221164
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ run-rustfix
2+
#![allow(unreachable_code, dead_code)]
3+
4+
fn main() {
5+
fn mismatch_types1() -> i32 {
6+
match 1 {
7+
x => return dbg!(x), //~ ERROR mismatched types
8+
}
9+
todo!()
10+
}
11+
12+
fn mismatch_types2() -> i32 {
13+
match 2 {
14+
x => {
15+
return dbg!(x) //~ ERROR mismatched types
16+
}
17+
}
18+
todo!()
19+
}
20+
21+
fn mismatch_types3() -> i32 {
22+
match 1 {
23+
_ => return dbg!(1) //~ ERROR mismatched types
24+
}
25+
todo!()
26+
}
27+
28+
fn mismatch_types4() -> i32 {
29+
match 1 {
30+
_ => {return dbg!(1)} //~ ERROR mismatched types
31+
}
32+
todo!()
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ run-rustfix
2+
#![allow(unreachable_code, dead_code)]
3+
4+
fn main() {
5+
fn mismatch_types1() -> i32 {
6+
match 1 {
7+
x => dbg!(x), //~ ERROR mismatched types
8+
}
9+
todo!()
10+
}
11+
12+
fn mismatch_types2() -> i32 {
13+
match 2 {
14+
x => {
15+
dbg!(x) //~ ERROR mismatched types
16+
}
17+
}
18+
todo!()
19+
}
20+
21+
fn mismatch_types3() -> i32 {
22+
match 1 {
23+
_ => dbg!(1) //~ ERROR mismatched types
24+
}
25+
todo!()
26+
}
27+
28+
fn mismatch_types4() -> i32 {
29+
match 1 {
30+
_ => {dbg!(1)} //~ ERROR mismatched types
31+
}
32+
todo!()
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/mismatched-types-issue-126222.rs:7:18
3+
|
4+
LL | x => dbg!(x),
5+
| ^^^^^^^ expected `()`, found integer
6+
|
7+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
help: you might have meant to return this value
9+
|
10+
LL | x => return dbg!(x),
11+
| ++++++
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/mismatched-types-issue-126222.rs:15:17
15+
|
16+
LL | dbg!(x)
17+
| ^^^^^^^ expected `()`, found integer
18+
|
19+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
20+
help: you might have meant to return this value
21+
|
22+
LL | return dbg!(x)
23+
| ++++++
24+
25+
error[E0308]: mismatched types
26+
--> $DIR/mismatched-types-issue-126222.rs:23:18
27+
|
28+
LL | _ => dbg!(1)
29+
| ^^^^^^^ expected `()`, found integer
30+
|
31+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
32+
help: you might have meant to return this value
33+
|
34+
LL | _ => return dbg!(1)
35+
| ++++++
36+
37+
error[E0308]: mismatched types
38+
--> $DIR/mismatched-types-issue-126222.rs:30:19
39+
|
40+
LL | _ => {dbg!(1)}
41+
| ^^^^^^^ expected `()`, found integer
42+
|
43+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
44+
help: you might have meant to return this value
45+
|
46+
LL | _ => {return dbg!(1)}
47+
| ++++++
48+
49+
error: aborting due to 4 previous errors
50+
51+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)