Skip to content

Commit 53cf6cf

Browse files
authored
Add parentheses for closure when suggesting calling closure
1 parent 3507a74 commit 53cf6cf

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -820,16 +820,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
820820
if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. })
821821
&& obligation.cause.span.can_be_used_for_suggestions()
822822
{
823+
let (span, sugg) = if let Some(snippet) =
824+
self.tcx.sess.source_map().span_to_snippet(obligation.cause.span).ok()
825+
&& snippet.starts_with("|")
826+
{
827+
(obligation.cause.span, format!("({snippet})({args})"))
828+
} else {
829+
(obligation.cause.span.shrink_to_hi(), format!("({args})"))
830+
};
831+
823832
// When the obligation error has been ensured to have been caused by
824833
// an argument, the `obligation.cause.span` points at the expression
825834
// of the argument, so we can provide a suggestion. Otherwise, we give
826835
// a more general note.
827-
err.span_suggestion_verbose(
828-
obligation.cause.span.shrink_to_hi(),
829-
msg,
830-
format!("({args})"),
831-
Applicability::HasPlaceholders,
832-
);
836+
err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
833837
} else if let DefIdOrName::DefId(def_id) = def_id_or_name {
834838
let name = match self.tcx.hir_get_if_local(def_id) {
835839
Some(hir::Node::Expr(hir::Expr {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ run-rustfix
2+
3+
use std::fmt::Display;
4+
5+
struct S;
6+
7+
impl S {
8+
fn call(&self, _: impl Display) {}
9+
}
10+
11+
fn main() {
12+
S.call((|| "hello")()); //~ ERROR [E0277]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ run-rustfix
2+
3+
use std::fmt::Display;
4+
5+
struct S;
6+
7+
impl S {
8+
fn call(&self, _: impl Display) {}
9+
}
10+
11+
fn main() {
12+
S.call(|| "hello"); //~ ERROR [E0277]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0277]: `{closure@$DIR/use-parentheses-to-call-closure-issue-145404.rs:12:12: 12:14}` doesn't implement `std::fmt::Display`
2+
--> $DIR/use-parentheses-to-call-closure-issue-145404.rs:12:12
3+
|
4+
LL | S.call(|| "hello");
5+
| ---- ^^^^^^^^^^ unsatisfied trait bound
6+
| |
7+
| required by a bound introduced by this call
8+
|
9+
= help: the trait `std::fmt::Display` is not implemented for closure `{closure@$DIR/use-parentheses-to-call-closure-issue-145404.rs:12:12: 12:14}`
10+
note: required by a bound in `S::call`
11+
--> $DIR/use-parentheses-to-call-closure-issue-145404.rs:8:28
12+
|
13+
LL | fn call(&self, _: impl Display) {}
14+
| ^^^^^^^ required by this bound in `S::call`
15+
help: use parentheses to call this closure
16+
|
17+
LL - S.call(|| "hello");
18+
LL + S.call((|| "hello")());
19+
|
20+
21+
error: aborting due to 1 previous error
22+
23+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)