Skip to content

Commit 74abd3d

Browse files
committed
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. Fixes #150693
1 parent aefa104 commit 74abd3d

File tree

2 files changed

+3
-11
lines changed

2 files changed

+3
-11
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5323,12 +5323,9 @@ fn hint_missing_borrow<'tcx>(
53235323
ty = mut_ty.ty;
53245324
left -= 1;
53255325
}
5326-
let sugg = if left == 0 {
5327-
(span, String::new())
5328-
} else {
5329-
(arg.span, expected_arg.to_string())
5330-
};
5331-
remove_borrow.push(sugg);
5326+
if left == 0 {
5327+
remove_borrow.push((span, String::new()));
5328+
}
53325329
}
53335330
}
53345331
}

tests/ui/closures/multiple-fn-bounds.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ note: required by a bound in `foo`
1919
|
2020
LL | fn foo<F: Fn(&char) -> bool + Fn(char) -> bool>(f: F) {
2121
| ^^^^^^^^^^^^^^^^ required by this bound in `foo`
22-
help: consider adjusting the signature so it does not borrow its argument
23-
|
24-
LL - foo(move |x| v);
25-
LL + foo(move |char| v);
26-
|
2722

2823
error: aborting due to 1 previous error
2924

0 commit comments

Comments
 (0)