Skip to content

Commit bf22d44

Browse files
committed
Don't suggest adding return type for closures with default return type
1 parent da7dd43 commit bf22d44

File tree

6 files changed

+25
-30
lines changed

6 files changed

+25
-30
lines changed

Diff for: compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
832832
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Unit { span });
833833
return true;
834834
}
835-
&hir::FnRetTy::DefaultReturn(span) if expected.is_unit() => {
835+
// Don't suggest adding return types for closures with default return.
836+
// They simply don't work.
837+
&hir::FnRetTy::DefaultReturn(span)
838+
if expected.is_unit() && !self.tcx.is_closure_like(fn_id.to_def_id()) =>
839+
{
836840
if let Some(found) = found.make_suggestable(self.tcx, false, None) {
837841
err.subdiagnostic(errors::AddReturnTypeSuggestion::Add {
838842
span,

Diff for: tests/ui/closures/add_semicolon_non_block_closure.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ fn main() {
88
foo(|| bar())
99
//~^ ERROR mismatched types [E0308]
1010
//~| HELP consider using a semicolon here
11-
//~| HELP try adding a return type
1211
}

Diff for: tests/ui/closures/add_semicolon_non_block_closure.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ help: consider using a semicolon here
88
|
99
LL | foo(|| { bar(); })
1010
| + +++
11-
help: try adding a return type
12-
|
13-
LL | foo(|| -> i32 bar())
14-
| ++++++
1511

1612
error: aborting due to 1 previous error
1713

Diff for: tests/ui/suggestions/try-operator-dont-suggest-semicolon.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
fn main() -> Result<(), ()> {
55
a(|| {
6-
//~^ HELP: try adding a return type
76
b()
87
//~^ ERROR: mismatched types [E0308]
98
//~| NOTE: expected `()`, found `i32`

Diff for: tests/ui/suggestions/try-operator-dont-suggest-semicolon.stderr

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
error[E0308]: mismatched types
2-
--> $DIR/try-operator-dont-suggest-semicolon.rs:7:9
2+
--> $DIR/try-operator-dont-suggest-semicolon.rs:6:9
33
|
44
LL | b()
5-
| ^^^ expected `()`, found `i32`
6-
|
7-
help: consider using a semicolon here
8-
|
9-
LL | b();
10-
| +
11-
help: try adding a return type
12-
|
13-
LL | a(|| -> i32 {
14-
| ++++++
5+
| ^^^- help: consider using a semicolon here: `;`
6+
| |
7+
| expected `()`, found `i32`
158

169
error[E0308]: mismatched types
17-
--> $DIR/try-operator-dont-suggest-semicolon.rs:17:9
10+
--> $DIR/try-operator-dont-suggest-semicolon.rs:16:9
1811
|
1912
LL | / if true {
2013
LL | |

Diff for: tests/ui/typeck/issue-81943.stderr

+15-11
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,36 @@ error[E0308]: mismatched types
22
--> $DIR/issue-81943.rs:7:9
33
|
44
LL | f(|x| lib::d!(x));
5-
| -^^^^^^^^^^ expected `()`, found `i32`
6-
| |
7-
| help: try adding a return type: `-> i32`
5+
| ^^^^^^^^^^ expected `()`, found `i32`
86
|
97
= note: this error originates in the macro `lib::d` (in Nightly builds, run with -Z macro-backtrace for more info)
108

119
error[E0308]: mismatched types
1210
--> $DIR/issue-81943.rs:8:28
1311
|
1412
LL | f(|x| match x { tmp => { g(tmp) } });
15-
| ^^^^^^ expected `()`, found `i32`
13+
| -------------------^^^^^^----
14+
| | |
15+
| | expected `()`, found `i32`
16+
| expected this to be `()`
1617
|
1718
help: consider using a semicolon here
1819
|
1920
LL | f(|x| match x { tmp => { g(tmp); } });
2021
| +
21-
help: try adding a return type
22+
help: consider using a semicolon here
2223
|
23-
LL | f(|x| -> i32 match x { tmp => { g(tmp) } });
24-
| ++++++
24+
LL | f(|x| match x { tmp => { g(tmp) } };);
25+
| +
2526

2627
error[E0308]: mismatched types
2728
--> $DIR/issue-81943.rs:10:38
2829
|
2930
LL | ($e:expr) => { match $e { x => { g(x) } } }
30-
| ^^^^ expected `()`, found `i32`
31+
| ------------------^^^^----
32+
| | |
33+
| | expected `()`, found `i32`
34+
| expected this to be `()`
3135
LL | }
3236
LL | f(|x| d!(x));
3337
| ----- in this macro invocation
@@ -37,10 +41,10 @@ help: consider using a semicolon here
3741
|
3842
LL | ($e:expr) => { match $e { x => { g(x); } } }
3943
| +
40-
help: try adding a return type
44+
help: consider using a semicolon here
4145
|
42-
LL | f(|x| -> i32 d!(x));
43-
| ++++++
46+
LL | ($e:expr) => { match $e { x => { g(x) } }; }
47+
| +
4448

4549
error: aborting due to 3 previous errors
4650

0 commit comments

Comments
 (0)