Skip to content

Commit b07ad64

Browse files
Suppress malformed closure return type suggestions
1 parent c94fef2 commit b07ad64

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -799,13 +799,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
799799
can_suggest: bool,
800800
fn_id: LocalDefId,
801801
) -> bool {
802-
// Can't suggest `->` on a block-like coroutine
802+
// Can't suggest `->` on a block-like coroutine.
803803
if let Some(hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Block)) =
804804
self.tcx.coroutine_kind(fn_id)
805805
{
806806
return false;
807807
}
808808

809+
// FIXME: Add braces to a closure when suggesting a return type.
810+
// A case where this may happen is `|| if { return x; }`, though we
811+
// should first think hard about whether these suggestions are useful.
812+
if let hir::Node::Expr(hir::Expr {
813+
kind: ExprKind::Closure(hir::Closure { body, .. }), ..
814+
}) = self.tcx.hir_node_by_def_id(fn_id)
815+
&& !matches!(self.tcx.hir().body(*body).value.kind, hir::ExprKind::Block(..))
816+
{
817+
return false;
818+
}
819+
809820
let found =
810821
self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(found));
811822
// Only suggest changing the return type for methods that

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)