Skip to content

Commit 1c4fa41

Browse files
committed
new_ret_no_self fix false positive for impl trait return with associated type self
1 parent 13ce96c commit 1c4fa41

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
934934
if let hir::ImplItemKind::Method(ref sig, id) = implitem.node {
935935
let ret_ty = return_ty(cx, implitem.id);
936936
if name == "new" &&
937-
!ret_ty.walk().any(|t| same_tys(cx, t, ty)) {
937+
!same_tys(cx, ret_ty, ty) &&
938+
!ret_ty.is_impl_trait() {
938939
span_lint(cx,
939940
NEW_RET_NO_SELF,
940941
implitem.span,

tests/ui/new_ret_no_self.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ impl S2 {
3535
}
3636
}
3737

38+
struct S3;
39+
40+
impl R for S3 {
41+
type Item = u32;
42+
}
43+
44+
impl S3 {
45+
// should trigger the lint, but currently does not
46+
pub fn new(_: String) -> impl R<Item = u32> {
47+
S3
48+
}
49+
}
50+
3851
struct T;
3952

4053
impl T {

tests/ui/new_ret_no_self.stderr

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
error: methods called `new` usually return `Self`
2-
--> $DIR/new_ret_no_self.rs:51:5
2+
--> $DIR/new_ret_no_self.rs:64:5
33
|
4-
51 | / pub fn new() -> u32 {
5-
52 | | unimplemented!();
6-
53 | | }
4+
64 | / pub fn new() -> u32 {
5+
65 | | unimplemented!();
6+
66 | | }
77
| |_____^
8+
|
9+
= note: `-D clippy::new-ret-no-self` implied by `-D warnings`
810

911
error: methods called `new` usually return `Self`
10-
--> $DIR/new_ret_no_self.rs:60:5
12+
--> $DIR/new_ret_no_self.rs:73:5
1113
|
12-
60 | / pub fn new(_: String) -> u32 {
13-
61 | | unimplemented!();
14-
62 | | }
14+
73 | / pub fn new(_: String) -> u32 {
15+
74 | | unimplemented!();
16+
75 | | }
1517
| |_____^
1618

1719
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)