Skip to content

Fix: false negative in useless_conversion on non-const items#16801

Open
Shivangisharma4 wants to merge 1 commit intorust-lang:masterfrom
Shivangisharma4:fix/issue-16794
Open

Fix: false negative in useless_conversion on non-const items#16801
Shivangisharma4 wants to merge 1 commit intorust-lang:masterfrom
Shivangisharma4:fix/issue-16794

Conversation

@Shivangisharma4
Copy link
Copy Markdown

@Shivangisharma4 Shivangisharma4 commented Apr 3, 2026

Fixes #16794

Background

In #14800, a fix was applied to suppress .into_iter() linting when the following chained method takes &mut self (like .any()). This successfully stopped a false positive when .into_iter() was called on const items (like a const R: Range = x..y), because removing the iteration call there triggers a const_item_mutation compiler warning (Issue #14656).

However, the guard added was too broad. It accidentally caused standard closures/expressions (like s.chars().into_iter().any(...)) to be entirely skipped by the useless_conversion lint.

What this PR does

  • Adds a is_const_item HIR helper to verify if the expression corresponds to Res::Def(DefKind::Const | DefKind::AssocConst).
  • Narrows the suppression guard by adding && is_const_item(cx, into_iter_recv) so that the strict checking only jumps in when protecting an actual const item.
  • All other standard variables properly trigger the lint when .into_iter() can be safely stripped.
  • Added regression tests in tests/ui/useless_conversion.rs to guard against this happening again.

Please write a short comment explaining your change (or "none" for internal only changes)

changelog: [useless_conversion]: fix false negative on non-const items when chaining &mut self methods

Copilot AI review requested due to automatic review settings April 3, 2026 09:51
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 3, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 3, 2026

r? @samueltardieu

rustbot has assigned @samueltardieu.
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

@rustbot

This comment has been minimized.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a regression in Clippy’s useless_conversion lint where .into_iter() calls were incorrectly skipped for non-const receivers when followed by &mut self iterator methods (e.g. .any()), while preserving the intended suppression for const items to avoid const_item_mutation warnings.

Changes:

  • Narrow useless_conversion’s suppression logic by introducing an is_const_item HIR helper and applying it to the &mut self + FnMut parent-method guard.
  • Add a regression test ensuring .chars().into_iter().any(...) correctly triggers useless_conversion.
  • Update UI test expectations (.stderr / .fixed) accordingly.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
clippy_lints/src/useless_conversion.rs Narrows the .into_iter() suppression to only apply when the receiver is a const/AssocConst item.
tests/ui/useless_conversion.rs Adds regression coverage for the non-const .into_iter().any(...) false negative while keeping const-item behavior covered.
tests/ui/useless_conversion.stderr Updates expected UI output to include the newly restored useless_conversion diagnostic.
tests/ui/useless_conversion.fixed Updates the rustfix expectation for the new regression case.
clippy_lints/src/eta_reduction.rs Adds suppression for redundant_closure_for_method_calls when the method originates from an ambiguous external crate name (diamond dependency / multiple versions).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

View changes since this review

Comment thread clippy_lints/src/eta_reduction.rs
Comment thread clippy_lints/src/eta_reduction.rs
@samueltardieu
Copy link
Copy Markdown
Member

Over review capacity RN
r? clippy

@rustbot rustbot assigned ada4a and unassigned samueltardieu Apr 5, 2026
@ada4a
Copy link
Copy Markdown
Contributor

ada4a commented Apr 5, 2026

I'm sorry, was this PR description and the comments in the code created by an LLM?

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.

False Negative, regression: useless_conversion not reported on .any()

5 participants