Skip to content

Commit 5a2a206

Browse files
committed
Don't lint default_trait_access in proc-macro expansions
1 parent 244b234 commit 5a2a206

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

clippy_lints/src/default.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg};
22
use clippy_utils::source::snippet_with_macro_callsite;
33
use clippy_utils::ty::{has_drop, is_copy};
4-
use clippy_utils::{any_parent_is_automatically_derived, contains_name, get_parent_expr, match_def_path, paths};
4+
use clippy_utils::{
5+
any_parent_is_automatically_derived, contains_name, get_parent_expr, is_from_proc_macro, match_def_path, paths,
6+
};
57
use if_chain::if_chain;
68
use rustc_data_structures::fx::FxHashSet;
79
use rustc_errors::Applicability;
@@ -94,6 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
9496
if let QPath::Resolved(None, _path) = qpath;
9597
let expr_ty = cx.typeck_results().expr_ty(expr);
9698
if let ty::Adt(def, ..) = expr_ty.kind();
99+
if !is_from_proc_macro(cx, expr);
97100
then {
98101
// TODO: Work out a way to put "whatever the imported way of referencing
99102
// this type in this file" rather than a fully-qualified type.

tests/ui/default_trait_access.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// run-rustfix
2+
// aux-build: proc_macro_with_span.rs
23

34
#![allow(unused_imports, dead_code)]
45
#![deny(clippy::default_trait_access)]
56

7+
extern crate proc_macro_with_span;
8+
9+
use proc_macro_with_span::with_span;
610
use std::default;
711
use std::default::Default as D2;
812
use std::string;
@@ -51,6 +55,8 @@ fn main() {
5155
..Default::default()
5256
};
5357

58+
let _s21: String = with_span!(s Default::default());
59+
5460
println!(
5561
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}]",
5662
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,

tests/ui/default_trait_access.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// run-rustfix
2+
// aux-build: proc_macro_with_span.rs
23

34
#![allow(unused_imports, dead_code)]
45
#![deny(clippy::default_trait_access)]
56

7+
extern crate proc_macro_with_span;
8+
9+
use proc_macro_with_span::with_span;
610
use std::default;
711
use std::default::Default as D2;
812
use std::string;
@@ -51,6 +55,8 @@ fn main() {
5155
..Default::default()
5256
};
5357

58+
let _s21: String = with_span!(s Default::default());
59+
5460
println!(
5561
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}]",
5662
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,

tests/ui/default_trait_access.stderr

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

1313
error: calling `std::string::String::default()` is more clear than this expression
14-
--> $DIR/default_trait_access.rs:15:22
14+
--> $DIR/default_trait_access.rs:19:22
1515
|
1616
LL | let s3: String = D2::default();
1717
| ^^^^^^^^^^^^^ help: try: `std::string::String::default()`
1818

1919
error: calling `std::string::String::default()` is more clear than this expression
20-
--> $DIR/default_trait_access.rs:17:22
20+
--> $DIR/default_trait_access.rs:21:22
2121
|
2222
LL | let s4: String = std::default::Default::default();
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
2424

2525
error: calling `std::string::String::default()` is more clear than this expression
26-
--> $DIR/default_trait_access.rs:21:22
26+
--> $DIR/default_trait_access.rs:25:22
2727
|
2828
LL | let s6: String = default::Default::default();
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
3030

3131
error: calling `GenericDerivedDefault::default()` is more clear than this expression
32-
--> $DIR/default_trait_access.rs:31:46
32+
--> $DIR/default_trait_access.rs:35:46
3333
|
3434
LL | let s11: GenericDerivedDefault<String> = Default::default();
3535
| ^^^^^^^^^^^^^^^^^^ help: try: `GenericDerivedDefault::default()`
3636

3737
error: calling `TupleDerivedDefault::default()` is more clear than this expression
38-
--> $DIR/default_trait_access.rs:37:36
38+
--> $DIR/default_trait_access.rs:41:36
3939
|
4040
LL | let s14: TupleDerivedDefault = Default::default();
4141
| ^^^^^^^^^^^^^^^^^^ help: try: `TupleDerivedDefault::default()`
4242

4343
error: calling `ArrayDerivedDefault::default()` is more clear than this expression
44-
--> $DIR/default_trait_access.rs:39:36
44+
--> $DIR/default_trait_access.rs:43:36
4545
|
4646
LL | let s15: ArrayDerivedDefault = Default::default();
4747
| ^^^^^^^^^^^^^^^^^^ help: try: `ArrayDerivedDefault::default()`
4848

4949
error: calling `TupleStructDerivedDefault::default()` is more clear than this expression
50-
--> $DIR/default_trait_access.rs:43:42
50+
--> $DIR/default_trait_access.rs:47:42
5151
|
5252
LL | let s17: TupleStructDerivedDefault = Default::default();
5353
| ^^^^^^^^^^^^^^^^^^ help: try: `TupleStructDerivedDefault::default()`

0 commit comments

Comments
 (0)