New lint: deref_method_call_chain#17402
Open
tanndlin wants to merge 4 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new Clippy methods lint, deref_method_call_chain, to detect and suggest removing redundant deref-like conversion calls (e.g., as_slice, as_str, as_path) before method calls when deref coercion would resolve the same inherent method.
Changes:
- Introduces the
deref_method_call_chainlint (declaration, registration, implementation) plus a dedicated UI test (with.fixedand.stderr). - Updates several existing UI tests to
allow(clippy::deref_method_call_chain)and refreshes expected.stderrline numbers. - Minor cleanup in
lintcheckto remove a redundant.as_os_str()in a method chain.
Reviewed changes
Copilot reviewed 25 out of 25 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/ui/return_and_then.stderr | Updated expected diagnostics line numbers after adding a crate-level allow. |
| tests/ui/return_and_then.rs | Adds #![allow(clippy::deref_method_call_chain)] to avoid new lint affecting this test. |
| tests/ui/return_and_then.fixed | Mirrors the new crate-level allow in the rustfix output. |
| tests/ui/redundant_as_str.stderr | Updated expected diagnostics line numbers after adding a crate-level allow. |
| tests/ui/redundant_as_str.rs | Adds #![allow(clippy::deref_method_call_chain)] to avoid interaction with new lint. |
| tests/ui/redundant_as_str.fixed | Mirrors the new crate-level allow in the rustfix output. |
| tests/ui/new_without_default.stderr | Updated expected diagnostics line numbers after adding a crate-level allow. |
| tests/ui/new_without_default.rs | Adds #![allow(clippy::deref_method_call_chain)]. |
| tests/ui/new_without_default.fixed | Mirrors the new crate-level allow in the rustfix output. |
| tests/ui/needless_for_each_fixable.stderr | Updated expected diagnostics line numbers after adding a crate-level allow. |
| tests/ui/needless_for_each_fixable.rs | Adds #![allow(clippy::deref_method_call_chain)]. |
| tests/ui/needless_for_each_fixable.fixed | Mirrors the new crate-level allow in the rustfix output. |
| tests/ui/manual_find_fixable.rs | Adds clippy::deref_method_call_chain to existing allow list. |
| tests/ui/manual_find_fixable.fixed | Mirrors the updated allow list in the rustfix output. |
| tests/ui/into_iter_without_iter.stderr | Updated expected diagnostics line numbers after adding a crate-level allow. |
| tests/ui/into_iter_without_iter.rs | Adds #![allow(clippy::deref_method_call_chain)]. |
| tests/ui/deref_method_call_chain.stderr | New UI stderr expectations for the new lint. |
| tests/ui/deref_method_call_chain.rs | New UI test cases covering std and user-defined deref-like conversions and exclusions. |
| tests/ui/deref_method_call_chain.fixed | New rustfix output expectations for the new lint. |
| lintcheck/src/main.rs | Removes a redundant as_os_str() call in a method chain. |
| clippy_utils/src/sym.rs | Adds extra symbols used by the new lint implementation/tests. |
| clippy_lints/src/methods/mod.rs | Declares, registers, and calls the new lint from the Methods pass. |
| clippy_lints/src/methods/deref_method_call_chain.rs | Implements the new lint logic and suggestion. |
| clippy_lints/src/declared_lints.rs | Adds the lint to the declared lint registry. |
| CHANGELOG.md | Adds a documentation link entry for the new lint. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+76
to
+84
| // The following method must be an inherent method of the deref target. Trait methods | ||
| // could resolve to a different impl on the original receiver (e.g. `IntoIterator` on | ||
| // `Vec<T>` vs `&[T]`), changing semantics | ||
| let Some(outer_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id) else { | ||
| return; | ||
| }; | ||
| let Some(impl_id) = cx.tcx.inherent_impl_of_assoc(outer_did) else { | ||
| return; | ||
| }; |
Contributor
Author
There was a problem hiding this comment.
Fixed and added test cases, but did not take suggested code
|
Lintcheck changes for ee6b42f
This comment will be updated if you push new changes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #2094
.stderrfile)cargo testpasses locallycargo dev update_lintscargo dev fmtchangelog: New lint [
deref_method_call_chain]