Fix: false negative in useless_conversion on non-const items#16801
Fix: false negative in useless_conversion on non-const items#16801Shivangisharma4 wants to merge 1 commit intorust-lang:masterfrom
Conversation
|
rustbot has assigned @samueltardieu. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
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 anis_const_itemHIR helper and applying it to the&mut self+FnMutparent-method guard. - Add a regression test ensuring
.chars().into_iter().any(...)correctly triggersuseless_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.
e092c60 to
191f93f
Compare
|
Over review capacity RN |
|
I'm sorry, was this PR description and the comments in the code created by an LLM? |
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 onconstitems (like aconst R: Range = x..y), because removing the iteration call there triggers aconst_item_mutationcompiler 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 theuseless_conversionlint.What this PR does
is_const_itemHIR helper to verify if the expression corresponds toRes::Def(DefKind::Const | DefKind::AssocConst).&& is_const_item(cx, into_iter_recv)so that the strict checking only jumps in when protecting an actual const item..into_iter()can be safely stripped.tests/ui/useless_conversion.rsto 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 selfmethods