Skip to content

Commit

Permalink
Don't suggest adding return type for closures with default return type
Browse files Browse the repository at this point in the history
  • Loading branch information
wafarm committed Aug 19, 2024
1 parent da7dd43 commit bf22d44
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 30 deletions.
6 changes: 5 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err.subdiagnostic(errors::ExpectedReturnTypeLabel::Unit { span });
return true;
}
&hir::FnRetTy::DefaultReturn(span) if expected.is_unit() => {
// Don't suggest adding return types for closures with default return.
// They simply don't work.
&hir::FnRetTy::DefaultReturn(span)
if expected.is_unit() && !self.tcx.is_closure_like(fn_id.to_def_id()) =>
{
if let Some(found) = found.make_suggestable(self.tcx, false, None) {
err.subdiagnostic(errors::AddReturnTypeSuggestion::Add {
span,
Expand Down
1 change: 0 additions & 1 deletion tests/ui/closures/add_semicolon_non_block_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ fn main() {
foo(|| bar())
//~^ ERROR mismatched types [E0308]
//~| HELP consider using a semicolon here
//~| HELP try adding a return type
}
4 changes: 0 additions & 4 deletions tests/ui/closures/add_semicolon_non_block_closure.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ help: consider using a semicolon here
|
LL | foo(|| { bar(); })
| + +++
help: try adding a return type
|
LL | foo(|| -> i32 bar())
| ++++++

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

fn main() -> Result<(), ()> {
a(|| {
//~^ HELP: try adding a return type
b()
//~^ ERROR: mismatched types [E0308]
//~| NOTE: expected `()`, found `i32`
Expand Down
17 changes: 5 additions & 12 deletions tests/ui/suggestions/try-operator-dont-suggest-semicolon.stderr
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
error[E0308]: mismatched types
--> $DIR/try-operator-dont-suggest-semicolon.rs:7:9
--> $DIR/try-operator-dont-suggest-semicolon.rs:6:9
|
LL | b()
| ^^^ expected `()`, found `i32`
|
help: consider using a semicolon here
|
LL | b();
| +
help: try adding a return type
|
LL | a(|| -> i32 {
| ++++++
| ^^^- help: consider using a semicolon here: `;`
| |
| expected `()`, found `i32`

error[E0308]: mismatched types
--> $DIR/try-operator-dont-suggest-semicolon.rs:17:9
--> $DIR/try-operator-dont-suggest-semicolon.rs:16:9
|
LL | / if true {
LL | |
Expand Down
26 changes: 15 additions & 11 deletions tests/ui/typeck/issue-81943.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,36 @@ error[E0308]: mismatched types
--> $DIR/issue-81943.rs:7:9
|
LL | f(|x| lib::d!(x));
| -^^^^^^^^^^ expected `()`, found `i32`
| |
| help: try adding a return type: `-> i32`
| ^^^^^^^^^^ expected `()`, found `i32`
|
= note: this error originates in the macro `lib::d` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
--> $DIR/issue-81943.rs:8:28
|
LL | f(|x| match x { tmp => { g(tmp) } });
| ^^^^^^ expected `()`, found `i32`
| -------------------^^^^^^----
| | |
| | expected `()`, found `i32`
| expected this to be `()`
|
help: consider using a semicolon here
|
LL | f(|x| match x { tmp => { g(tmp); } });
| +
help: try adding a return type
help: consider using a semicolon here
|
LL | f(|x| -> i32 match x { tmp => { g(tmp) } });
| ++++++
LL | f(|x| match x { tmp => { g(tmp) } };);
| +

error[E0308]: mismatched types
--> $DIR/issue-81943.rs:10:38
|
LL | ($e:expr) => { match $e { x => { g(x) } } }
| ^^^^ expected `()`, found `i32`
| ------------------^^^^----
| | |
| | expected `()`, found `i32`
| expected this to be `()`
LL | }
LL | f(|x| d!(x));
| ----- in this macro invocation
Expand All @@ -37,10 +41,10 @@ help: consider using a semicolon here
|
LL | ($e:expr) => { match $e { x => { g(x); } } }
| +
help: try adding a return type
help: consider using a semicolon here
|
LL | f(|x| -> i32 d!(x));
| ++++++
LL | ($e:expr) => { match $e { x => { g(x) } }; }
| +

error: aborting due to 3 previous errors

Expand Down

0 comments on commit bf22d44

Please sign in to comment.