Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

derivable_impls false positive on nongeneric impl #7655

Closed
dtolnay opened this issue Sep 10, 2021 · 2 comments · Fixed by #7660
Closed

derivable_impls false positive on nongeneric impl #7655

dtolnay opened this issue Sep 10, 2021 · 2 comments · Fixed by #7660
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@dtolnay
Copy link
Member

dtolnay commented Sep 10, 2021

pub struct Collection<T> {
    v: Vec<T>,
}

impl Default for Collection<String> {
    fn default() -> Self {
        Collection {
            v: Vec::new(),
        }
    }
}
$ cargo clippy

warning: this `impl` can be derived
  --> src/main.rs:5:1
   |
5  | / impl Default for Collection<String> {
6  | |     fn default() -> Self {
7  | |         Collection {
8  | |             v: Vec::new(),
9  | |         }
10 | |     }
11 | | }
   | |_^
   |
   = note: `#[warn(clippy::derivable_impls)]` on by default
   = help: try annotating `Collection` with `#[derive(Default)]`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls

Clippy's suggestion is not equivalent to the handwritten impl. It is more generic which may not be desirable.

fn main() {
    let _ = Collection::default(); // works fine with handwritten Default
}

ambiguity with derived default but not handwritten default:

error[E0282]: type annotations needed for `Collection<T>`
 --> src/main.rs:7:13
  |
7 |     let _ = Collection::default();
  |         -   ^^^^^^^^^^ cannot infer type for type parameter `T` declared on the struct `Collection`
  |         |
  |         consider giving this pattern the explicit type `Collection<T>`, where the type parameter `T` is specified

@HKalbasi @camsteffen

Meta

Rust version (rustc -Vv):

rustc 1.57.0-nightly (497ee321a 2021-09-09)
binary: rustc
commit-hash: 497ee321af3b8496eaccd7af7b437f18bab81abf
commit-date: 2021-09-09
host: x86_64-unknown-linux-gnu
release: 1.57.0-nightly
LLVM version: 13.0.0
@dtolnay dtolnay added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Sep 10, 2021
dtolnay added a commit to serde-rs/json that referenced this issue Sep 10, 2021
rust-lang/rust-clippy#7655

    error: this `impl` can be derived
       --> src/map.rs:254:1
        |
    254 | / impl Default for Map<String, Value> {
    255 | |     #[inline]
    256 | |     fn default() -> Self {
    257 | |         Map {
    ...   |
    260 | |     }
    261 | | }
        | |_^
        |
        = note: `#[deny(clippy::derivable_impls)]` implied by `#[deny(clippy::all)]`
        = help: try annotating `map::Map` with `#[derive(Default)]`
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls
@HKalbasi
Copy link
Member

@camsteffen Shouldn't this line handle it?

if cx.tcx.type_of(item.def_id).definitely_has_param_types_or_consts(cx.tcx) {

@camsteffen
Copy link
Contributor

I'm guessing that returns false because the type param is substituted with String and we need to go back to inspectingtype_of().kind().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants