rust-lang / rust-lang/rust-clippy

this `impl` can be derived

Open
#15,970 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

It looks like clippy is confused by presence of cfg in enum type and still offers to implement Default using derive macro which is unlikely to work.

Lint Name

derivable_impls

Reproducer

I tried this code:

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum WireguardMultihopMode {
    /// Multihop using two tun devices to nest tunnels.
    #[cfg(not(any(target_os = "android", target_os = "ios")))]
    TunTun,

    /// Netstack based multihop.
    Netstack,
}

impl Default for WireguardMultihopMode {
    fn default() -> Self {
        #[cfg(any(target_os = "ios", target_os = "android"))]
        {
            Self::Netstack
        }

        #[cfg(not(any(target_os = "android", target_os = "ios")))]
        {
            Self::TunTun
        }
    }
}

I saw this happen:

warning: this `impl` can be derived
   --> crates\nym-vpn-lib\src\tunnel_state_machine\mod.rs:211:1
    |
211 | / impl Default for WireguardMultihopMode {
212 | |     fn default() -> Self {
213 | |         #[cfg(any(target_os = "ios", target_os = "android"))]
...   |
223 | | }
    | |_^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls
    = note: `#[warn(clippy::derivable_impls)]` on by default
help: replace the manual implementation with a derive attribute and mark the default variant
    |
202 + #[derive(Default)]
203 | pub enum WireguardMultihopMode {
204 |     /// Multihop using two tun devices to nest tunnels.
205 |     #[cfg(not(any(target_os = "android", target_os = "ios")))]
206 ~     #[default]
207 ~     TunTun,
    |

warning: `nym-vpn-lib` (lib) generated 1 warning (run `cargo clippy --fix --lib -p nym-vpn-lib` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 47.82s

I expected to see this happen:

Nothing.

Version
rustc 1.93.0-nightly (f37aa9955 2025-10-26)
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the derivable_impls lint and reproduce the supplied enum with its cfg-gated variants on the affected targets. Check the lint's existing tests, if present, and add coverage for this case; done means Clippy emits neither the warning nor the derive suggestion when the suggested default variant is unavailable.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.