Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions clippy_lints/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ impl<'tcx> LateLintPass<'tcx> for Default {
// Detect and ignore <Foo as Default>::default() because these calls do explicitly name the type.
&& let QPath::Resolved(None, _path) = qpath
&& let expr_ty = cx.typeck_results().expr_ty(expr)
&& let ty::Adt(def, ..) = expr_ty.kind()
&& let ty::Adt(_, args) = expr_ty.kind()
&& !is_from_proc_macro(cx, expr)
{
let replacement = with_forced_trimmed_paths!(format!("{}::default()", cx.tcx.def_path_str(def.did())));
let mut replacement = with_forced_trimmed_paths!(format!("{}::default()", expr_ty));
if !args.is_empty() {
replacement = with_forced_trimmed_paths!(format!("<{}>::default()", expr_ty));
}
span_lint_and_sugg(
cx,
DEFAULT_TRAIT_ACCESS,
Expand Down
24 changes: 23 additions & 1 deletion tests/ui/default_trait_access.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {

let s10 = DerivedDefault::default();

let s11: GenericDerivedDefault<String> = GenericDerivedDefault::default();
let s11: GenericDerivedDefault<String> = <GenericDerivedDefault<String>>::default();
//~^ default_trait_access

let s12 = GenericDerivedDefault::<String>::default();
Expand Down Expand Up @@ -69,6 +69,28 @@ fn main() {
);
}

fn issue16325() {
use std::borrow::Cow;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::{Arc, RwLock};

#[derive(Default, Clone)]
struct Foo;

let _: Arc<Foo> = <Arc<Foo>>::default();
//~^ default_trait_access

let _: Rc<Foo> = <Rc<Foo>>::default();
//~^ default_trait_access

let _: Cow<'_, Foo> = <Cow<'_, Foo>>::default();
//~^ default_trait_access

let _: Arc<RwLock<HashMap<String, Foo>>> = <Arc<RwLock<HashMap<String, Foo>>>>::default();
//~^ default_trait_access
}

struct DefaultFactory;

impl DefaultFactory {
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/default_trait_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ fn main() {
);
}

fn issue16325() {
use std::borrow::Cow;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::{Arc, RwLock};

#[derive(Default, Clone)]
struct Foo;

let _: Arc<Foo> = Default::default();
//~^ default_trait_access

let _: Rc<Foo> = Default::default();
//~^ default_trait_access

let _: Cow<'_, Foo> = Default::default();
//~^ default_trait_access

let _: Arc<RwLock<HashMap<String, Foo>>> = Default::default();
//~^ default_trait_access
}

struct DefaultFactory;

impl DefaultFactory {
Expand Down
30 changes: 27 additions & 3 deletions tests/ui/default_trait_access.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ error: calling `String::default()` is more clear than this expression
LL | let s6: String = default::Default::default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `String::default()`

error: calling `GenericDerivedDefault::default()` is more clear than this expression
error: calling `<GenericDerivedDefault<String>>::default()` is more clear than this expression
--> tests/ui/default_trait_access.rs:36:46
|
LL | let s11: GenericDerivedDefault<String> = Default::default();
| ^^^^^^^^^^^^^^^^^^ help: try: `GenericDerivedDefault::default()`
| ^^^^^^^^^^^^^^^^^^ help: try: `<GenericDerivedDefault<String>>::default()`

error: calling `TupleDerivedDefault::default()` is more clear than this expression
--> tests/ui/default_trait_access.rs:43:36
Expand All @@ -52,5 +52,29 @@ error: calling `TupleStructDerivedDefault::default()` is more clear than this ex
LL | let s17: TupleStructDerivedDefault = Default::default();
| ^^^^^^^^^^^^^^^^^^ help: try: `TupleStructDerivedDefault::default()`

error: aborting due to 8 previous errors
error: calling `<Arc<Foo>>::default()` is more clear than this expression
--> tests/ui/default_trait_access.rs:81:23
|
LL | let _: Arc<Foo> = Default::default();
| ^^^^^^^^^^^^^^^^^^ help: try: `<Arc<Foo>>::default()`

error: calling `<Rc<Foo>>::default()` is more clear than this expression
--> tests/ui/default_trait_access.rs:84:22
|
LL | let _: Rc<Foo> = Default::default();
| ^^^^^^^^^^^^^^^^^^ help: try: `<Rc<Foo>>::default()`

error: calling `<Cow<'_, Foo>>::default()` is more clear than this expression
--> tests/ui/default_trait_access.rs:87:27
|
LL | let _: Cow<'_, Foo> = Default::default();
| ^^^^^^^^^^^^^^^^^^ help: try: `<Cow<'_, Foo>>::default()`

error: calling `<Arc<RwLock<HashMap<String, Foo>>>>::default()` is more clear than this expression
--> tests/ui/default_trait_access.rs:90:48
|
LL | let _: Arc<RwLock<HashMap<String, Foo>>> = Default::default();
| ^^^^^^^^^^^^^^^^^^ help: try: `<Arc<RwLock<HashMap<String, Foo>>>>::default()`

error: aborting due to 12 previous errors

Loading