From 1c4fa419f33211db3fa60f3bc8d59a8f42992558 Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Wed, 3 Oct 2018 04:59:14 -0700 Subject: [PATCH] new_ret_no_self fix false positive for impl trait return with associated type self --- clippy_lints/src/methods/mod.rs | 3 ++- tests/ui/new_ret_no_self.rs | 13 +++++++++++++ tests/ui/new_ret_no_self.stderr | 18 ++++++++++-------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 81cb1cd1182..7426ece5ba9 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -934,7 +934,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { if let hir::ImplItemKind::Method(ref sig, id) = implitem.node { let ret_ty = return_ty(cx, implitem.id); if name == "new" && - !ret_ty.walk().any(|t| same_tys(cx, t, ty)) { + !same_tys(cx, ret_ty, ty) && + !ret_ty.is_impl_trait() { span_lint(cx, NEW_RET_NO_SELF, implitem.span, diff --git a/tests/ui/new_ret_no_self.rs b/tests/ui/new_ret_no_self.rs index 762dd582168..3b7ff7780ef 100644 --- a/tests/ui/new_ret_no_self.rs +++ b/tests/ui/new_ret_no_self.rs @@ -35,6 +35,19 @@ impl S2 { } } +struct S3; + +impl R for S3 { + type Item = u32; +} + +impl S3 { + // should trigger the lint, but currently does not + pub fn new(_: String) -> impl R { + S3 + } +} + struct T; impl T { diff --git a/tests/ui/new_ret_no_self.stderr b/tests/ui/new_ret_no_self.stderr index 1d698892449..cab5fa55cb6 100644 --- a/tests/ui/new_ret_no_self.stderr +++ b/tests/ui/new_ret_no_self.stderr @@ -1,17 +1,19 @@ error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:51:5 + --> $DIR/new_ret_no_self.rs:64:5 | -51 | / pub fn new() -> u32 { -52 | | unimplemented!(); -53 | | } +64 | / pub fn new() -> u32 { +65 | | unimplemented!(); +66 | | } | |_____^ + | + = note: `-D clippy::new-ret-no-self` implied by `-D warnings` error: methods called `new` usually return `Self` - --> $DIR/new_ret_no_self.rs:60:5 + --> $DIR/new_ret_no_self.rs:73:5 | -60 | / pub fn new(_: String) -> u32 { -61 | | unimplemented!(); -62 | | } +73 | / pub fn new(_: String) -> u32 { +74 | | unimplemented!(); +75 | | } | |_____^ error: aborting due to 2 previous errors