rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

New lint unnecessary_dedup_by

Open xtqqczze opened this issue 10 months ago • 1 comments

What it does

Like unnecessary_sort_by but for Vec::dedup_by instead of Vec::sort_by.

This lint detects cases where dedup_by is used with a comparison that is equivalent to a simple key extraction and could be written more idiomatically using dedup_by_key.

Example

pub fn m1(mut v: Vec<(i64, i64)>) -> Vec<(i64, i64)> {
    v.dedup_by(|(sym1, _), (sym2, _)| sym1 == sym2);
    v
}

Could be written as:

pub fn m2(mut v: Vec<(i64, i64)>) -> Vec<(i64, i64)> {
    v.dedup_by_key(|(sym, _)| *sym);
    v
}

Alternatives

This functionality could instead be folded into unnecessary_sort_by, with the lint renamed or generalized to cover both sorting and deduplication cases.

xtqqczze avatar Jan 28 '26 06:01 xtqqczze

@rustbot claim

moses7054 avatar Jan 29 '26 11:01 moses7054