From afe76df79cd43d510cc7e746e7402edf8f8eed58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Heath=20Dutton=F0=9F=95=B4=EF=B8=8F?= Date: Mon, 12 Jan 2026 18:04:32 -0500 Subject: [PATCH] Don't suggest replacing closure parameter with type name When a closure has an inferred parameter type like `|ch|` and the expected type differs in borrowing (e.g., `char` vs `&char`), the suggestion code would incorrectly suggest `|char|` instead of the valid `|ch: char|`. This happened because the code couldn't walk explicit `&` references in the HIR when the type is inferred, and fell back to replacing the entire parameter span with the expected type name. Fix by only emitting the suggestion when we can properly identify the `&` syntax to remove. --- .../src/error_reporting/traits/suggestions.rs | 9 +++------ tests/ui/closures/multiple-fn-bounds.stderr | 5 ----- 2 files changed, 3 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 511e9e85b5f6..81b921b3744f 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs @@ -5323,12 +5323,9 @@ fn hint_missing_borrow<'tcx>( ty = mut_ty.ty; left -= 1; } - let sugg = if left == 0 { - (span, String::new()) - } else { - (arg.span, expected_arg.to_string()) - }; - remove_borrow.push(sugg); + if left == 0 { + remove_borrow.push((span, String::new())); + } } } } diff --git a/tests/ui/closures/multiple-fn-bounds.stderr b/tests/ui/closures/multiple-fn-bounds.stderr index 9b824fa0eefb..c99cbac01faf 100644 --- a/tests/ui/closures/multiple-fn-bounds.stderr +++ b/tests/ui/closures/multiple-fn-bounds.stderr @@ -19,11 +19,6 @@ note: required by a bound in `foo` | LL | fn foo bool + Fn(char) -> bool>(f: F) { | ^^^^^^^^^^^^^^^^ required by this bound in `foo` -help: consider adjusting the signature so it does not borrow its argument - | -LL - foo(move |x| v); -LL + foo(move |char| v); - | error: aborting due to 1 previous error