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

default_trait_access should only fire on simple types #5975

Closed
adrianheine opened this issue Aug 27, 2020 · 6 comments · Fixed by #5993
Closed

default_trait_access should only fire on simple types #5975

adrianheine opened this issue Aug 27, 2020 · 6 comments · Fixed by #5993
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@adrianheine
Copy link

Calling std::cell::RefCell<std::collections::HashMap<R, futures::future::Shared<std::pin::Pin<std::boxed::Box<dyn futures::Future<Output = ()>>>>>>::default() is more clear than this expression

I don't agree

Calling std::marker::PhantomData<(L, B)>::default() is more clear than this expression

For PhantomData I think I always prefer Default::default().

@flip1995
Copy link
Member

Can you provide example code please?

@adrianheine
Copy link
Author

Sure!

#![deny(clippy::default_trait_access)]
use std::cell::RefCell;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;

struct X {
    c: RefCell<HashMap<String, Pin<Box<dyn Future<Output = ()>>>>>
}

fn main() {
    let x = X { c: Default::default() };
}

gives

error: calling `std::cell::RefCell<std::collections::HashMap<std::string::String, std::pin::Pin<std::boxed::Box<dyn std::future::Future<Output = ()>>>>>::default()` is more clear than this expression
  --> src/main.rs:12:20
   |
12 |     let x = X { c: Default::default() };
   |                    ^^^^^^^^^^^^^^^^^^ help: try: `std::cell::RefCell<std::collections::HashMap<std::string::String, std::pin::Pin<std::boxed::Box<dyn std::future::Future<Output = ()>>>>>::default()`
   |

@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Aug 31, 2020
@flip1995
Copy link
Member

Thanks!

We should add a configuration option where the type depth can be specified. About the PhantomData case: This lint is pedantic and therefore meant to be opinionated, so I wouldn't change that.

@taiki-e
Copy link
Member

taiki-e commented Aug 31, 2020

| ^^^^^^^^^^^^^^^^^^ help: try: std::cell::RefCell<std::collections::HashMap<std::string::String, std::pin::Pin<std::boxed::Box<dyn std::future::Future<Output = ()>>>>>::default()

I think the underlying problem is clippy suggests code with complete parameters, not clippy triggers this lint even for complex types. AFAIK, If code compiles with Default::default, it doesn't need to specify any parameters, as type inference is working. (So, in this case, default_trait_access should suggest RefCell::default.)

@adrianheine
Copy link
Author

adrianheine commented Aug 31, 2020

That's a very good observation, thanks! See also #5990 about this.

I think the nicest would be to explicitly include all types up to the first type that can be »empty«, i. e. whose Default implementation does not depend on some other non-optional type parameter being Default. In this case, that would be RefCell<HashMap>::default(), but that's not valid syntax. RefCell::<HashMap<_, _>>::default() looks ugly. As such, I agree with your suggestion to just use the out-most type name. That's also something the lint already accepts, so this is really just about the suggested solution.

@taiki-e
Copy link
Member

taiki-e commented Aug 31, 2020

This and #5990 will be fixed in #5993

@bors bors closed this as completed in 67e18c2 Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants