Skip to content

Allow explicit_deref_methods in Deref impls #15392

@konstin

Description

@konstin

Summary

A common pattern in Rust is forwarding the members of an enum to their inner implementation:

impl Display for FetchUrl { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { match self { Self::Index(index) => Display::fmt(index, f), Self::Realm(realm) => Display::fmt(realm, f), } } }
impl Debug for OptionSet { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { Display::fmt(self, f) } }

We use the fully-qualified Display::fmt or Debug::fmt to avoid ambiguity over which trait's method we are calling.

When using the same pattern for Deref on nightly, explicit_deref_methods triggers:

impl Deref for SyncEnvironment { type Target = PythonEnvironment; fn deref(&self) -> &Self::Target { match self { Self::Project(environment) => Deref::deref(environment), Self::Script(environment) => Deref::deref(environment), } } }

The lint description says:

This lint excludes all of:

let _ = d.unwrap().deref(); let _ = Foo::deref(&foo); let _ = <Foo as Deref>::deref(&foo); 

Maybe this should be extended to Deref::deref, especially in Deref impls?

Lint Name

explicit_deref_methods

Reproducer

I tried this code:

impl Deref for SyncEnvironment { type Target = PythonEnvironment; fn deref(&self) -> &Self::Target { match self { Self::Project(environment) => Deref::deref(environment), Self::Script(environment) => Deref::deref(environment), } } }

I saw this happen:

warning: explicit `deref` method call --> crates/uv/src/commands/project/sync.rs:549:42 | 549 | Self::Script(environment) => Deref::deref(environment), | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&**environment` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_deref_methods = note: `-W clippy::explicit-deref-methods` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::explicit_deref_methods)]` 

I expected to see this happen:

No error.

Version

rustc 1.90.0-nightly (4b55fe199 2025-08-01) binary: rustc commit-hash: 4b55fe199cfe9c710555a5af7f2a49491ad38254 commit-date: 2025-08-01 host: x86_64-unknown-linux-gnu release: 1.90.0-nightly LLVM version: 20.1.8 

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions