Skip to content

Too much path qualification in recommended fix #7220

Closed
@kinnison

Description

@kinnison

The new_without_default lint (indirectly) points at an impl block containing the new() method and suggests something to insert. However because the name is "fully qualified" if you insert the suggested impl Default alongside the impl block, it will not compile.

I tried this code:

pub mod submod {
    pub struct Foo<T> {
        _bar: *mut T,
    }

    impl<T> Foo<T> {
        pub fn new() -> Self {
            todo!()
        }
    }
}

I expected to see this happen:

warning: you should consider adding a `Default` implementation for `submod::Foo<T>`
 --> src/lib.rs:7:9
  |
7 | /         pub fn new() -> Self {
8 | |             todo!()
9 | |         }
  | |_________^
  |
  = note: `#[warn(clippy::new_without_default)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default
help: try this
  |
6 |     impl<T> Default for Foo<T> {
7 |         fn default() -> Self {
8 |             Self::new()
9 |         }
10|     }
11| 
...

Instead, this happened (specifically note the qualified submod::Foo in the suggested impl):

warning: you should consider adding a `Default` implementation for `submod::Foo<T>`
 --> src/lib.rs:7:9
  |
7 | /         pub fn new() -> Self {
8 | |             todo!()
9 | |         }
  | |_________^
  |
  = note: `#[warn(clippy::new_without_default)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#new_without_default
help: try this
  |
6 |     impl<T> Default for submod::Foo<T> {
7 |         fn default() -> Self {
8 |             Self::new()
9 |         }
10|     }
11| 
...

Meta

  • cargo clippy -V: clippy 0.1.54 (5c02926 2021-05-11)
  • rustc -Vv:

rustc 1.54.0-nightly (5c02926 2021-05-11)
binary: rustc
commit-hash: 5c02926
commit-date: 2021-05-11
host: x86_64-unknown-linux-gnu
release: 1.54.0-nightly
LLVM version: 12.0.1




<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"xFrednet"}$$TRIAGEBOT_ASSIGN_DATA_END -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions