Skip to content

Commit b33cc09

Browse files
committed
Don't lint default_trait_access in proc-macro expansions
1 parent 7af8376 commit b33cc09

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

clippy_lints/src/default.rs

+4-1
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.

clippy_lints/src/unit_types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ declare_clippy_lint! {
9898

9999
declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE, UNIT_CMP, UNIT_ARG]);
100100

101-
impl LateLintPass<'tcx> for UnitTypes {
101+
impl<'tcx> LateLintPass<'tcx> for UnitTypes {
102102
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
103103
let_unit_value::check(cx, local);
104104
}

clippy_utils/src/check_proc_macro.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_ast::ast::{IntTy, LitIntType, LitKind, StrStyle, UintTy};
1616
use rustc_hir::{
1717
Block, BlockCheckMode, Destination, Expr, ExprKind, FieldDef, FnHeader, Impl, ImplItem, ImplItemKind, IsAuto, Item,
1818
ItemKind, LoopSource, MatchSource, QPath, TraitItem, TraitItemKind, UnOp, UnsafeSource, Unsafety, Variant,
19-
VariantData, VisibilityKind, YieldSource,
19+
VariantData, YieldSource,
2020
};
2121
use rustc_lint::{LateContext, LintContext};
2222
use rustc_middle::ty::TyCtxt;
@@ -196,7 +196,7 @@ fn item_search_pat(item: &Item<'_>) -> (Pat, Pat) {
196196
ItemKind::Impl(_) => (Pat::Str("impl"), Pat::Str("}")),
197197
_ => return (Pat::Str(""), Pat::Str("")),
198198
};
199-
if matches!(item.vis.node, VisibilityKind::Inherited) {
199+
if item.vis_span.is_empty() {
200200
(start_pat, end_pat)
201201
} else {
202202
(Pat::Str("pub"), end_pat)
@@ -217,15 +217,15 @@ fn impl_item_search_pat(item: &ImplItem<'_>) -> (Pat, Pat) {
217217
ImplItemKind::TyAlias(..) => (Pat::Str("type"), Pat::Str(";")),
218218
ImplItemKind::Fn(sig, ..) => (fn_header_search_pat(sig.header), Pat::Str("")),
219219
};
220-
if matches!(item.vis.node, VisibilityKind::Inherited) {
220+
if item.vis_span.is_empty() {
221221
(start_pat, end_pat)
222222
} else {
223223
(Pat::Str("pub"), end_pat)
224224
}
225225
}
226226

227227
fn field_def_search_pat(def: &FieldDef<'_>) -> (Pat, Pat) {
228-
if matches!(def.vis.node, VisibilityKind::Inherited) {
228+
if def.vis_span.is_empty() {
229229
if def.is_positional() {
230230
(Pat::Str(""), Pat::Str(""))
231231
} else {

tests/ui/default_trait_access.fixed

+6
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

+6
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

+9-9
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)