rust-lang / rust-lang/rust-clippy

clippy::default_trait_access suggests solution that won't compile

Open
#8,342 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

Having something like this

mod document {
    #[derive(Default)]
    pub struct Document;
    impl Document {
        pub fn new() -> Self {
            Default::default()
        }
    }
}

Results in the following warning:

    Checking off-rs v0.1.0 (C:\Users\michi\development\private\off-rs)
warning: calling `document::Document::default()` is more clear than this expression
  --> src\document.rs:73:9
   |
73 |         Default::default()
   |         ^^^^^^^^^^^^^^^^^^ help: try: `document::Document::default()`
   |
note: the lint level is defined here
  --> src\lib.rs:1:9
   |
1  | #![warn(clippy::pedantic)]
   |         ^^^^^^^^^^^^^^^^
   = note: `#[warn(clippy::default_trait_access)]` implied by `#[warn(clippy::pedantic)]`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#default_trait_access

But this suggestion will result in a failing build:

    Checking off-rs v0.1.0 (C:\Users\michi\development\private\off-rs)
error[E0433]: failed to resolve: use of undeclared crate or module `document`
  --> src\document.rs:73:9
   |
73 |         document::Document::default()
   |         ^^^^^^^^ use of undeclared crate or module `document`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `off-rs` due to previous error
Reproducer

Just run clippy on https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e8effe414c02059d94aa622e27ec8fff

Version
rustc 1.58.1 (db9d1b20b 2022-01-20)
binary: rustc
commit-hash: db9d1b20bba1968c1ec1fc49616d4742c1725b4b
commit-date: 2022-01-20
host: x86_64-pc-windows-msvc
release: 1.58.1
LLVM version: 13.0.0
Additional Labels

@rustbot label +l-suggestion-causes-error

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

Run the linked Rust Playground reproducer and trace the clippy::default_trait_access lint where it constructs its suggestion. Verify the replacement in the issue's nested-module example, and consider the issue complete when the suggested code compiles in that context without losing the lint's intended guidance.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.