diff options
| author | Mu001999 <mu001999@outlook.com> | 2025-08-16 15:07:38 +0800 |
|---|---|---|
| committer | Mu001999 <mu001999@outlook.com> | 2025-08-16 15:07:38 +0800 |
| commit | 5e8c2c3aec19c50efc22854c09564844a133bbaf (patch) | |
| tree | d5f12d712e4efc69b0c359849c74d0680b29e17f | |
| parent | 57feb7fc018cd91cc89b7777184cbafc548367b0 (diff) | |
| download | rust-5e8c2c3aec19c50efc22854c09564844a133bbaf.tar.gz rust-5e8c2c3aec19c50efc22854c09564844a133bbaf.zip | |
Add parentheses for closure when suggesting calling closure
4 files changed, 17 insertions, 11 deletions
diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs index bc8c8a44405..3bcbeddf95c 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -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 { diff --git a/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr b/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr index 4c8a5e46751..c05584ef909 100644 --- a/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr +++ b/tests/ui/const-generics/adt_const_params/const_param_ty_bad.stderr @@ -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 diff --git a/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.fixed b/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.fixed index 4d29b9b0b1a..2835743fca2 100644 --- a/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.fixed +++ b/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.fixed @@ -9,5 +9,5 @@ impl S { } fn main() { - S.call(|| "hello"()); //~ ERROR [E0277] + S.call((|| "hello")()); //~ ERROR [E0277] } diff --git a/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.stderr b/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.stderr index ee51dbca804..cb6df5af7fb 100644 --- a/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.stderr +++ b/tests/ui/suggestions/use-parentheses-to-call-closure-issue-145404.stderr @@ -14,8 +14,9 @@ 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"); +LL + S.call((|| "hello")()); + | error: aborting due to 1 previous error |
