Skip to content

Fix unnecessary_sort_by reverse suggestion using wrong closure parameter name#16868

Open
laffed wants to merge 1 commit intorust-lang:masterfrom
laffed:fix-unnecessary-sort-by-param-name
Open

Fix unnecessary_sort_by reverse suggestion using wrong closure parameter name#16868
laffed wants to merge 1 commit intorust-lang:masterfrom
laffed:fix-unnecessary-sort-by-param-name

Conversation

@laffed
Copy link
Copy Markdown

@laffed laffed commented Apr 16, 2026

When suggesting sort_by_key for a reversed comparator like |a, b| b.foo.cmp(&a.foo), the lint was picking left_expr (the receiver of .cmp(), which references b) and r_pat as the closure arg, producing |b| Reverse(b.foo). Both sides consistently named b, but b is the second parameter — confusing and inconsistent with the forward-sort suggestion style.

Fixed by using right_expr (the .cmp() argument, which references a) as the key body and l_pat as the closure parameter, peeling the single & that .cmp(&rhs) introduces so the suggestion doesn't contain a spurious borrow.


changelog: [unnecessary_sort_by]: fix reverse-sort suggestion using second closure parameter name (b) instead of first (a)

…t suggestion

When suggesting `sort_by_key` for a reversed comparator like  `|a, b|
b.foo.cmp(&a.foo)`, the lint was picking `left_expr` (the  receiver of
`.cmp()`, which references `b`) and `r_pat` as the closure  arg,
producing `|b| Reverse(b.foo)`. Both sides consistently named `b`,  but
`b` is the second parameter — confusing and inconsistent with the
forward-sort suggestion style.  Fix by using `right_expr` (the `.cmp()`
argument, which references `a`)  as the key body and `l_pat` as the
closure parameter, peeling the single  `&` that `.cmp(&rhs)` introduces
so the suggestion doesn't contain a  spurious borrow.
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 16, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 16, 2026

r? @Jarcho

rustbot has assigned @Jarcho.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants