From 350036a0c7bd64bd49049f1864a575fad9216677 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Mon, 27 Aug 2018 23:22:07 +0100 Subject: [PATCH 1/3] default_trait_access skips ::default() This includes the type name, so is clear, and may be necessary. There doesn't seem to be an obviously cleaner way to pull out the literal text of the named type here. Fixes #2879 --- clippy_lints/src/default_trait_access.rs | 7 +++++++ tests/ui/default_trait_access.rs | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/default_trait_access.rs b/clippy_lints/src/default_trait_access.rs index f01e106df26..d3598a5bdaa 100644 --- a/clippy_lints/src/default_trait_access.rs +++ b/clippy_lints/src/default_trait_access.rs @@ -48,6 +48,13 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { then { match qpath { QPath::Resolved(..) => { + if let ExprKind::Call(ref method, ref _args) = expr.node { + if format!("{:?}", method).contains(" as Default>") { + return + } + } + + // TODO: Work out a way to put "whatever the imported way of referencing // this type in this file" rather than a fully-qualified type. let expr_ty = cx.tables.expr_ty(expr); diff --git a/tests/ui/default_trait_access.rs b/tests/ui/default_trait_access.rs index 675e64246fa..eba024353f9 100644 --- a/tests/ui/default_trait_access.rs +++ b/tests/ui/default_trait_access.rs @@ -41,8 +41,10 @@ fn main() { let s18 = TupleStructDerivedDefault::default(); + let s19 = ::default(); + println!( - "[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}]", + "[{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}] [{:?}], [{:?}]", s1, s2, s3, @@ -61,6 +63,7 @@ fn main() { s16, s17, s18, + s19, ); } From 368223a341059c235e44ead210788c0047a4e6ad Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Sun, 2 Sep 2018 23:37:28 +0100 Subject: [PATCH 2/3] Use types rather than strings --- clippy_lints/src/default_trait_access.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/default_trait_access.rs b/clippy_lints/src/default_trait_access.rs index d3598a5bdaa..83b824631bc 100644 --- a/clippy_lints/src/default_trait_access.rs +++ b/clippy_lints/src/default_trait_access.rs @@ -48,13 +48,18 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { then { match qpath { QPath::Resolved(..) => { - if let ExprKind::Call(ref method, ref _args) = expr.node { - if format!("{:?}", method).contains(" as Default>") { - return + if_chain! { + // Detect and ignore ::default() because these calls do + // explicitly name the type. + if let ExprKind::Call(ref method, ref _args) = expr.node; + if let ExprKind::Path(ref p) = method.node; + if let QPath::Resolved(ref ty, ref _path) = p; + if ty.is_some(); + then { + return; } } - // TODO: Work out a way to put "whatever the imported way of referencing // this type in this file" rather than a fully-qualified type. let expr_ty = cx.tables.expr_ty(expr); From 939d842ea1a8732c86cbaed76e5283358e831efa Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Sun, 2 Sep 2018 23:42:07 +0100 Subject: [PATCH 3/3] Simplify --- clippy_lints/src/default_trait_access.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clippy_lints/src/default_trait_access.rs b/clippy_lints/src/default_trait_access.rs index 83b824631bc..c8c4d6cb873 100644 --- a/clippy_lints/src/default_trait_access.rs +++ b/clippy_lints/src/default_trait_access.rs @@ -53,8 +53,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { // explicitly name the type. if let ExprKind::Call(ref method, ref _args) = expr.node; if let ExprKind::Path(ref p) = method.node; - if let QPath::Resolved(ref ty, ref _path) = p; - if ty.is_some(); + if let QPath::Resolved(Some(_ty), _path) = p; then { return; }