Skip to content

Add parentheses for closure when suggesting calling closure #145481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -820,16 +820,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if matches!(obligation.cause.code(), ObligationCauseCode::FunctionArg { .. })
&& obligation.cause.span.can_be_used_for_suggestions()
{
let (span, sugg) = if let Some(snippet) =
self.tcx.sess.source_map().span_to_snippet(obligation.cause.span).ok()
&& snippet.starts_with("|")
{
(obligation.cause.span, format!("({snippet})({args})"))
} else {
(obligation.cause.span.shrink_to_hi(), format!("({args})"))
};

// When the obligation error has been ensured to have been caused by
// an argument, the `obligation.cause.span` points at the expression
// of the argument, so we can provide a suggestion. Otherwise, we give
// a more general note.
err.span_suggestion_verbose(
obligation.cause.span.shrink_to_hi(),
msg,
format!("({args})"),
Applicability::HasPlaceholders,
);
err.span_suggestion_verbose(span, msg, sugg, Applicability::HasPlaceholders);
} else if let DefIdOrName::DefId(def_id) = def_id_or_name {
let name = match self.tcx.hir_get_if_local(def_id) {
Some(hir::Node::Expr(hir::Expr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ LL | fn check(_: impl std::marker::UnsizedConstParamTy) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check`
help: use parentheses to call this closure
|
LL | check(|| {}());
| ++
LL - check(|| {});
LL + check((|| {})());
|

error[E0277]: `fn()` can't be used as a const parameter type
--> $DIR/const_param_ty_bad.rs:9:11
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ run-rustfix

use std::fmt::Display;

struct S;

impl S {
fn call(&self, _: impl Display) {}
}

fn main() {
S.call((|| "hello")()); //~ ERROR [E0277]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ run-rustfix

use std::fmt::Display;

struct S;

impl S {
fn call(&self, _: impl Display) {}
}

fn main() {
S.call(|| "hello"); //~ ERROR [E0277]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0277]: `{closure@$DIR/use-parentheses-to-call-closure-issue-145404.rs:12:12: 12:14}` doesn't implement `std::fmt::Display`
--> $DIR/use-parentheses-to-call-closure-issue-145404.rs:12:12
|
LL | S.call(|| "hello");
| ---- ^^^^^^^^^^ unsatisfied trait bound
| |
| required by a bound introduced by this call
|
= 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}`
note: required by a bound in `S::call`
--> $DIR/use-parentheses-to-call-closure-issue-145404.rs:8:28
|
LL | fn call(&self, _: impl Display) {}
| ^^^^^^^ required by this bound in `S::call`
help: use parentheses to call this closure
|
LL - S.call(|| "hello");
LL + S.call((|| "hello")());
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.
Loading