Skip to content

fix: unnecessary_to_owned FP when map key is a reference #14834

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

Merged
merged 1 commit into from
May 18, 2025
Merged
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
22 changes: 16 additions & 6 deletions clippy_lints/src/methods/unnecessary_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,11 +655,18 @@ fn is_to_string_on_string_like<'a>(
}
}

fn is_a_std_map_type(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
is_type_diagnostic_item(cx, ty, sym::HashSet)
|| is_type_diagnostic_item(cx, ty, sym::HashMap)
|| is_type_diagnostic_item(cx, ty, sym::BTreeMap)
|| is_type_diagnostic_item(cx, ty, sym::BTreeSet)
fn std_map_key<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
match ty.kind() {
ty::Adt(adt, args)
if matches!(
cx.tcx.get_diagnostic_name(adt.did()),
Some(sym::BTreeMap | sym::BTreeSet | sym::HashMap | sym::HashSet)
) =>
{
Some(args.type_at(0))
},
_ => None,
}
}

fn is_str_and_string(cx: &LateContext<'_>, arg_ty: Ty<'_>, original_arg_ty: Ty<'_>) -> bool {
Expand Down Expand Up @@ -721,6 +728,7 @@ fn check_if_applicable_to_argument<'tcx>(cx: &LateContext<'tcx>, arg: &Expr<'tcx
// 1. This is a method with only one argument that doesn't come from a trait.
// 2. That it has `Borrow` in its generic predicates.
// 3. `Self` is a std "map type" (ie `HashSet`, `HashMap`, `BTreeSet`, `BTreeMap`).
// 4. The key to the "map type" is not a reference.
fn check_borrow_predicate<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
if let ExprKind::MethodCall(_, caller, &[arg], _) = expr.kind
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
Expand All @@ -738,7 +746,9 @@ fn check_borrow_predicate<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
})
&& let caller_ty = cx.typeck_results().expr_ty(caller)
// For now we limit it to "map types".
&& is_a_std_map_type(cx, caller_ty)
&& let Some(key_ty) = std_map_key(cx, caller_ty)
// We need to check that the key type is not a reference.
&& !key_ty.is_ref()
{
check_if_applicable_to_argument(cx, &arg);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/unnecessary_to_owned.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,9 @@ mod issue_14242 {
rc_slice_provider().to_vec().into_iter()
}
}

fn issue14833() {
use std::collections::HashSet;
let mut s = HashSet::<&String>::new();
s.remove(&"hello".to_owned());
}
6 changes: 6 additions & 0 deletions tests/ui/unnecessary_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,9 @@ mod issue_14242 {
rc_slice_provider().to_vec().into_iter()
}
}

fn issue14833() {
use std::collections::HashSet;
let mut s = HashSet::<&String>::new();
s.remove(&"hello".to_owned());
}