Skip to content

Commit

Permalink
Don't lint default_trait_access in proc-macro expansions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Jul 2, 2022
1 parent 7af8376 commit b33cc09
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
5 changes: 4 additions & 1 deletion clippy_lints/src/default.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg};
use clippy_utils::source::snippet_with_macro_callsite;
use clippy_utils::ty::{has_drop, is_copy};
use clippy_utils::{any_parent_is_automatically_derived, contains_name, get_parent_expr, match_def_path, paths};
use clippy_utils::{
any_parent_is_automatically_derived, contains_name, get_parent_expr, is_from_proc_macro, match_def_path, paths,
};
use if_chain::if_chain;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -94,6 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
if let QPath::Resolved(None, _path) = qpath;
let expr_ty = cx.typeck_results().expr_ty(expr);
if let ty::Adt(def, ..) = expr_ty.kind();
if !is_from_proc_macro(cx, expr);
then {
// TODO: Work out a way to put "whatever the imported way of referencing
// this type in this file" rather than a fully-qualified type.
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/unit_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ declare_clippy_lint! {

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

impl LateLintPass<'tcx> for UnitTypes {
impl<'tcx> LateLintPass<'tcx> for UnitTypes {
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
let_unit_value::check(cx, local);
}
Expand Down
8 changes: 4 additions & 4 deletions clippy_utils/src/check_proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_ast::ast::{IntTy, LitIntType, LitKind, StrStyle, UintTy};
use rustc_hir::{
Block, BlockCheckMode, Destination, Expr, ExprKind, FieldDef, FnHeader, Impl, ImplItem, ImplItemKind, IsAuto, Item,
ItemKind, LoopSource, MatchSource, QPath, TraitItem, TraitItemKind, UnOp, UnsafeSource, Unsafety, Variant,
VariantData, VisibilityKind, YieldSource,
VariantData, YieldSource,
};
use rustc_lint::{LateContext, LintContext};
use rustc_middle::ty::TyCtxt;
Expand Down Expand Up @@ -196,7 +196,7 @@ fn item_search_pat(item: &Item<'_>) -> (Pat, Pat) {
ItemKind::Impl(_) => (Pat::Str("impl"), Pat::Str("}")),
_ => return (Pat::Str(""), Pat::Str("")),
};
if matches!(item.vis.node, VisibilityKind::Inherited) {
if item.vis_span.is_empty() {
(start_pat, end_pat)
} else {
(Pat::Str("pub"), end_pat)
Expand All @@ -217,15 +217,15 @@ fn impl_item_search_pat(item: &ImplItem<'_>) -> (Pat, Pat) {
ImplItemKind::TyAlias(..) => (Pat::Str("type"), Pat::Str(";")),
ImplItemKind::Fn(sig, ..) => (fn_header_search_pat(sig.header), Pat::Str("")),
};
if matches!(item.vis.node, VisibilityKind::Inherited) {
if item.vis_span.is_empty() {
(start_pat, end_pat)
} else {
(Pat::Str("pub"), end_pat)
}
}

fn field_def_search_pat(def: &FieldDef<'_>) -> (Pat, Pat) {
if matches!(def.vis.node, VisibilityKind::Inherited) {
if def.vis_span.is_empty() {
if def.is_positional() {
(Pat::Str(""), Pat::Str(""))
} else {
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/default_trait_access.fixed
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// run-rustfix
// aux-build: proc_macro_with_span.rs

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

extern crate proc_macro_with_span;

use proc_macro_with_span::with_span;
use std::default;
use std::default::Default as D2;
use std::string;
Expand Down Expand Up @@ -51,6 +55,8 @@ fn main() {
..Default::default()
};

let _s21: String = with_span!(s Default::default());

println!(
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}]",
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/default_trait_access.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// run-rustfix
// aux-build: proc_macro_with_span.rs

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

extern crate proc_macro_with_span;

use proc_macro_with_span::with_span;
use std::default;
use std::default::Default as D2;
use std::string;
Expand Down Expand Up @@ -51,6 +55,8 @@ fn main() {
..Default::default()
};

let _s21: String = with_span!(s Default::default());

println!(
"[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}]",
s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20,
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/default_trait_access.stderr
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
error: calling `std::string::String::default()` is more clear than this expression
--> $DIR/default_trait_access.rs:11:22
--> $DIR/default_trait_access.rs:15:22
|
LL | let s1: String = Default::default();
| ^^^^^^^^^^^^^^^^^^ help: try: `std::string::String::default()`
|
note: the lint level is defined here
--> $DIR/default_trait_access.rs:4:9
--> $DIR/default_trait_access.rs:5:9
|
LL | #![deny(clippy::default_trait_access)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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

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

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

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

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

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

0 comments on commit b33cc09

Please sign in to comment.