Rollup merge of #151035 - issue-150693-closure-arg-suggestion, r=jieyouxu

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 would incorrectly propose `|char|` instead of something valid like `|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.

Fixes rust-lang/rust#150693
This commit is contained in:
Jonathan Brouwer 2026-01-13 09:01:35 +01:00 committed by GitHub
commit 7ee6257470
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 3 additions and 11 deletions

View file

@ -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()));
}
}
}
}

View file

@ -19,11 +19,6 @@ note: required by a bound in `foo`
|
LL | fn foo<F: Fn(&char) -> 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