Skip to content

Commit 5e8c2c3

Browse files
committed
Add parentheses for closure when suggesting calling closure
1 parent 57feb7f commit 5e8c2c3

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
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 {

tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ LL | fn check(_: impl std::marker::UnsizedConstParamTy) {}
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check`
3333
help: use parentheses to call this closure
3434
|
35-
LL | check(|| {}());
36-
| ++
35+
LL - check(|| {});
36+
LL + check((|| {})());
37+
|
3738

3839
error[E0277]: `fn()` can't be used as a const parameter type
3940
--> $DIR/const_param_ty_bad.rs:9:11

tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ impl S {
99
}
1010

1111
fn main() {
12-
S.call(|| "hello"()); //~ ERROR [E0277]
12+
S.call((|| "hello")()); //~ ERROR [E0277]
1313
}

tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ LL | fn call(&self, _: impl Display) {}
1414
| ^^^^^^^ required by this bound in `S::call`
1515
help: use parentheses to call this closure
1616
|
17-
LL | S.call(|| "hello"());
18-
| ++
17+
LL - S.call(|| "hello");
18+
LL + S.call((|| "hello")());
19+
|
1920

2021
error: aborting due to 1 previous error
2122

0 commit comments

Comments
 (0)