Skip to content

Commit

Permalink
new_ret_no_self fix false positive for impl trait return with associa…
Browse files Browse the repository at this point in the history
…ted type self
  • Loading branch information
JoshMcguigan committed Oct 13, 2018
1 parent 13ce96c commit 1c4fa41
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/methods/mod.rs
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/new_ret_no_self.rs
Expand Up @@ -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<Item = u32> {
S3
}
}

struct T;

impl T {
Expand Down
18 changes: 10 additions & 8 deletions 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
Expand Down

0 comments on commit 1c4fa41

Please sign in to comment.