Skip to content

Commit b24d753

Browse files
committed
Exclude generated code
1 parent d312473 commit b24d753

File tree

4 files changed

+113
-15
lines changed

4 files changed

+113
-15
lines changed

clippy_lints/src/default_trait_access.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use rustc::hir::*;
22
use rustc::lint::*;
3+
use rustc::ty::TypeVariants;
34

4-
use crate::utils::{match_def_path, opt_def_id, paths, span_lint_and_sugg};
5+
use crate::utils::{any_parent_is_automatically_derived, match_def_path, opt_def_id, paths, span_lint_and_sugg};
56

67

78
/// **What it does:** Checks for literal calls to `Default::default()`.
@@ -38,6 +39,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
3839
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
3940
if_chain! {
4041
if let ExprCall(ref path, ..) = expr.node;
42+
if !any_parent_is_automatically_derived(cx.tcx, expr.id);
4143
if let ExprPath(ref qpath) = path.node;
4244
if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id));
4345
if match_def_path(cx.tcx, def_id, &paths::DEFAULT_TRAIT_METHOD);
@@ -46,14 +48,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
4648
QPath::Resolved(..) => {
4749
// TODO: Work out a way to put "whatever the imported way of referencing
4850
// this type in this file" rather than a fully-qualified type.
49-
let replacement = format!("{}::default()", cx.tables.expr_ty(expr));
50-
span_lint_and_sugg(
51-
cx,
52-
DEFAULT_TRAIT_ACCESS,
53-
expr.span,
54-
&format!("Calling {} is more clear than this expression", replacement),
55-
"try",
56-
replacement);
51+
let expr_ty = cx.tables.expr_ty(expr);
52+
if let TypeVariants::TyAdt(..) = expr_ty.sty {
53+
let replacement = format!("{}::default()", expr_ty);
54+
span_lint_and_sugg(
55+
cx,
56+
DEFAULT_TRAIT_ACCESS,
57+
expr.span,
58+
&format!("Calling {} is more clear than this expression", replacement),
59+
"try",
60+
replacement);
61+
}
5762
},
5863
QPath::TypeRelative(..) => {},
5964
}

clippy_lints/src/utils/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,3 +1128,17 @@ pub fn without_block_comments(lines: Vec<&str>) -> Vec<&str> {
11281128

11291129
without
11301130
}
1131+
1132+
pub fn any_parent_is_automatically_derived(tcx: TyCtxt, node: NodeId) -> bool {
1133+
let map = &tcx.hir;
1134+
let mut prev_enclosing_node = None;
1135+
let mut enclosing_node = node;
1136+
while Some(enclosing_node) != prev_enclosing_node {
1137+
if is_automatically_derived(map.attrs(enclosing_node)) {
1138+
return true;
1139+
}
1140+
prev_enclosing_node = Some(enclosing_node);
1141+
enclosing_node = map.get_parent(enclosing_node);
1142+
}
1143+
false
1144+
}

tests/ui/default_trait_access.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,45 @@ fn main() {
2323

2424
let s9: String = DefaultFactory::make_t_nicely();
2525

26-
println!("[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}]", s1, s2, s3, s4, s5, s6, s7, s8, s9);
26+
let s10 = DerivedDefault::default();
27+
28+
let s11: GenericDerivedDefault<String> = Default::default();
29+
30+
let s12 = GenericDerivedDefault::<String>::default();
31+
32+
let s13 = TupleDerivedDefault::default();
33+
34+
let s14: TupleDerivedDefault = Default::default();
35+
36+
let s15: ArrayDerivedDefault = Default::default();
37+
38+
let s16 = ArrayDerivedDefault::default();
39+
40+
let s17: TupleStructDerivedDefault = Default::default();
41+
42+
let s18 = TupleStructDerivedDefault::default();
43+
44+
println!(
45+
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}]",
46+
s1,
47+
s2,
48+
s3,
49+
s4,
50+
s5,
51+
s6,
52+
s7,
53+
s8,
54+
s9,
55+
s10,
56+
s11,
57+
s12,
58+
s13,
59+
s14,
60+
s15,
61+
s16,
62+
s17,
63+
s18,
64+
);
2765
}
2866

2967
struct DefaultFactory;
@@ -37,3 +75,26 @@ impl DefaultFactory {
3775
T::default()
3876
}
3977
}
78+
79+
#[derive(Debug, Default)]
80+
struct DerivedDefault {
81+
pub s: String,
82+
}
83+
84+
#[derive(Debug, Default)]
85+
struct GenericDerivedDefault<T: Default + std::fmt::Debug> {
86+
pub s: T,
87+
}
88+
89+
#[derive(Debug, Default)]
90+
struct TupleDerivedDefault {
91+
pub s: (String, String),
92+
}
93+
94+
#[derive(Debug, Default)]
95+
struct ArrayDerivedDefault {
96+
pub s: [String; 10],
97+
}
98+
99+
#[derive(Debug, Default)]
100+
struct TupleStructDerivedDefault(String);

tests/ui/default_trait_access.stderr

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,29 @@ error: Calling std::string::String::default() is more clear than this expression
2424
18 | let s6: String = default::Default::default();
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
2626

27-
error: Calling T::default() is more clear than this expression
28-
--> $DIR/default_trait_access.rs:33:9
27+
error: Calling GenericDerivedDefault<std::string::String>::default() is more clear than this expression
28+
--> $DIR/default_trait_access.rs:28:46
2929
|
30-
33 | Default::default()
31-
| ^^^^^^^^^^^^^^^^^^ help: try: `T::default()`
30+
28 | let s11: GenericDerivedDefault<String> = Default::default();
31+
| ^^^^^^^^^^^^^^^^^^ help: try: `GenericDerivedDefault<std::string::String>::default()`
3232

33-
error: aborting due to 5 previous errors
33+
error: Calling TupleDerivedDefault::default() is more clear than this expression
34+
--> $DIR/default_trait_access.rs:34:36
35+
|
36+
34 | let s14: TupleDerivedDefault = Default::default();
37+
| ^^^^^^^^^^^^^^^^^^ help: try: `TupleDerivedDefault::default()`
38+
39+
error: Calling ArrayDerivedDefault::default() is more clear than this expression
40+
--> $DIR/default_trait_access.rs:36:36
41+
|
42+
36 | let s15: ArrayDerivedDefault = Default::default();
43+
| ^^^^^^^^^^^^^^^^^^ help: try: `ArrayDerivedDefault::default()`
44+
45+
error: Calling TupleStructDerivedDefault::default() is more clear than this expression
46+
--> $DIR/default_trait_access.rs:40:42
47+
|
48+
40 | let s17: TupleStructDerivedDefault = Default::default();
49+
| ^^^^^^^^^^^^^^^^^^ help: try: `TupleStructDerivedDefault::default()`
50+
51+
error: aborting due to 8 previous errors
3452

0 commit comments

Comments
 (0)