Skip to content

New lint: deref_method_call_chain#17402

Open
tanndlin wants to merge 4 commits into
rust-lang:masterfrom
tanndlin:deref_method_call_chain
Open

New lint: deref_method_call_chain#17402
tanndlin wants to merge 4 commits into
rust-lang:masterfrom
tanndlin:deref_method_call_chain

Conversation

@tanndlin

@tanndlin tanndlin commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

fixes #2094

  • Followed [lint naming conventions][lint_naming]
  • Added passing UI tests (including committed .stderr file)
  • cargo test passes locally
  • Executed cargo dev update_lints
  • Added lint documentation
  • Run cargo dev fmt

changelog: New lint [deref_method_call_chain]

Copilot AI review requested due to automatic review settings July 11, 2026 04:00
@rustbot rustbot added S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jul 11, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_chain lint (declaration, registration, implementation) plus a dedicated UI test (with .fixed and .stderr).
  • Updates several existing UI tests to allow(clippy::deref_method_call_chain) and refreshes expected .stderr line numbers.
  • Minor cleanup in lintcheck to 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.

View changes since this review

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;
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed and added test cases, but did not take suggested code

@github-actions

github-actions Bot commented Jul 11, 2026

Copy link
Copy Markdown

Lintcheck changes for ee6b42f

Lint Added Removed Changed
clippy::deref_method_call_chain 14 0 0

This comment will be updated if you push new changes

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

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. 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.

Lint calling methods that are redundant to deref

3 participants