Skip to content

Commit 67e18c2

Browse files
committed
Auto merge of rust-lang#5993 - taiki-e:default_trait_access, r=phansch
default_trait_access: Fix wrong suggestion rust-lang/rust-clippy#5975 (comment) > 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`.) Fixes rust-lang#5975 Fixes rust-lang#5990 changelog: `default_trait_access`: fixed wrong suggestion
2 parents 066f105 + 8b0aa6a commit 67e18c2

File tree

4 files changed

+127
-14
lines changed

4 files changed

+127
-14
lines changed

clippy_lints/src/default_trait_access.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ impl<'tcx> LateLintPass<'tcx> for DefaultTraitAccess {
5555
// TODO: Work out a way to put "whatever the imported way of referencing
5656
// this type in this file" rather than a fully-qualified type.
5757
let expr_ty = cx.typeck_results().expr_ty(expr);
58-
if let ty::Adt(..) = expr_ty.kind {
59-
let replacement = format!("{}::default()", expr_ty);
58+
if let ty::Adt(def, ..) = expr_ty.kind {
59+
let replacement = format!("{}::default()", cx.tcx.def_path_str(def.did));
6060
span_lint_and_sugg(
6161
cx,
6262
DEFAULT_TRAIT_ACCESS,

tests/ui/default_trait_access.fixed

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// run-rustfix
2+
3+
#![allow(unused_imports)]
4+
#![deny(clippy::default_trait_access)]
5+
6+
use std::default;
7+
use std::default::Default as D2;
8+
use std::string;
9+
10+
fn main() {
11+
let s1: String = std::string::String::default();
12+
13+
let s2 = String::default();
14+
15+
let s3: String = std::string::String::default();
16+
17+
let s4: String = std::string::String::default();
18+
19+
let s5 = string::String::default();
20+
21+
let s6: String = std::string::String::default();
22+
23+
let s7 = std::string::String::default();
24+
25+
let s8: String = DefaultFactory::make_t_badly();
26+
27+
let s9: String = DefaultFactory::make_t_nicely();
28+
29+
let s10 = DerivedDefault::default();
30+
31+
let s11: GenericDerivedDefault<String> = GenericDerivedDefault::default();
32+
33+
let s12 = GenericDerivedDefault::<String>::default();
34+
35+
let s13 = TupleDerivedDefault::default();
36+
37+
let s14: TupleDerivedDefault = TupleDerivedDefault::default();
38+
39+
let s15: ArrayDerivedDefault = ArrayDerivedDefault::default();
40+
41+
let s16 = ArrayDerivedDefault::default();
42+
43+
let s17: TupleStructDerivedDefault = TupleStructDerivedDefault::default();
44+
45+
let s18 = TupleStructDerivedDefault::default();
46+
47+
let s19 = <DerivedDefault as Default>::default();
48+
49+
println!(
50+
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}], [{:?}]",
51+
s1,
52+
s2,
53+
s3,
54+
s4,
55+
s5,
56+
s6,
57+
s7,
58+
s8,
59+
s9,
60+
s10,
61+
s11,
62+
s12,
63+
s13,
64+
s14,
65+
s15,
66+
s16,
67+
s17,
68+
s18,
69+
s19,
70+
);
71+
}
72+
73+
struct DefaultFactory;
74+
75+
impl DefaultFactory {
76+
pub fn make_t_badly<T: Default>() -> T {
77+
Default::default()
78+
}
79+
80+
pub fn make_t_nicely<T: Default>() -> T {
81+
T::default()
82+
}
83+
}
84+
85+
#[derive(Debug, Default)]
86+
struct DerivedDefault {
87+
pub s: String,
88+
}
89+
90+
#[derive(Debug, Default)]
91+
struct GenericDerivedDefault<T: Default + std::fmt::Debug> {
92+
pub s: T,
93+
}
94+
95+
#[derive(Debug, Default)]
96+
struct TupleDerivedDefault {
97+
pub s: (String, String),
98+
}
99+
100+
#[derive(Debug, Default)]
101+
struct ArrayDerivedDefault {
102+
pub s: [String; 10],
103+
}
104+
105+
#[derive(Debug, Default)]
106+
struct TupleStructDerivedDefault(String);

tests/ui/default_trait_access.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#![warn(clippy::default_trait_access)]
1+
// run-rustfix
2+
3+
#![allow(unused_imports)]
4+
#![deny(clippy::default_trait_access)]
25

36
use std::default;
47
use std::default::Default as D2;

tests/ui/default_trait_access.stderr

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,53 @@
11
error: calling `std::string::String::default()` is more clear than this expression
2-
--> $DIR/default_trait_access.rs:8:22
2+
--> $DIR/default_trait_access.rs:11:22
33
|
44
LL | let s1: String = Default::default();
55
| ^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
66
|
7-
= note: `-D clippy::default-trait-access` implied by `-D warnings`
7+
note: the lint level is defined here
8+
--> $DIR/default_trait_access.rs:4:9
9+
|
10+
LL | #![deny(clippy::default_trait_access)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
812

913
error: calling `std::string::String::default()` is more clear than this expression
10-
--> $DIR/default_trait_access.rs:12:22
14+
--> $DIR/default_trait_access.rs:15:22
1115
|
1216
LL | let s3: String = D2::default();
1317
| ^^^^^^^^^^^^^ help: try: `std::string::String::default()`
1418

1519
error: calling `std::string::String::default()` is more clear than this expression
16-
--> $DIR/default_trait_access.rs:14:22
20+
--> $DIR/default_trait_access.rs:17:22
1721
|
1822
LL | let s4: String = std::default::Default::default();
1923
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
2024

2125
error: calling `std::string::String::default()` is more clear than this expression
22-
--> $DIR/default_trait_access.rs:18:22
26+
--> $DIR/default_trait_access.rs:21:22
2327
|
2428
LL | let s6: String = default::Default::default();
2529
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
2630

27-
error: calling `GenericDerivedDefault<std::string::String>::default()` is more clear than this expression
28-
--> $DIR/default_trait_access.rs:28:46
31+
error: calling `GenericDerivedDefault::default()` is more clear than this expression
32+
--> $DIR/default_trait_access.rs:31:46
2933
|
3034
LL | let s11: GenericDerivedDefault<String> = Default::default();
31-
| ^^^^^^^^^^^^^^^^^^ help: try: `GenericDerivedDefault<std::string::String>::default()`
35+
| ^^^^^^^^^^^^^^^^^^ help: try: `GenericDerivedDefault::default()`
3236

3337
error: calling `TupleDerivedDefault::default()` is more clear than this expression
34-
--> $DIR/default_trait_access.rs:34:36
38+
--> $DIR/default_trait_access.rs:37:36
3539
|
3640
LL | let s14: TupleDerivedDefault = Default::default();
3741
| ^^^^^^^^^^^^^^^^^^ help: try: `TupleDerivedDefault::default()`
3842

3943
error: calling `ArrayDerivedDefault::default()` is more clear than this expression
40-
--> $DIR/default_trait_access.rs:36:36
44+
--> $DIR/default_trait_access.rs:39:36
4145
|
4246
LL | let s15: ArrayDerivedDefault = Default::default();
4347
| ^^^^^^^^^^^^^^^^^^ help: try: `ArrayDerivedDefault::default()`
4448

4549
error: calling `TupleStructDerivedDefault::default()` is more clear than this expression
46-
--> $DIR/default_trait_access.rs:40:42
50+
--> $DIR/default_trait_access.rs:43:42
4751
|
4852
LL | let s17: TupleStructDerivedDefault = Default::default();
4953
| ^^^^^^^^^^^^^^^^^^ help: try: `TupleStructDerivedDefault::default()`

0 commit comments

Comments
 (0)