Skip to content

Commit 13ce96c

Browse files
committed
new_ret_no_self corrected panic and added test stderr
1 parent eb854b2 commit 13ce96c

File tree

3 files changed

+65
-45
lines changed

3 files changed

+65
-45
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -931,13 +931,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
931931
}
932932
}
933933

934-
let ret_ty = return_ty(cx, implitem.id);
935-
if name == "new" &&
936-
!ret_ty.walk().any(|t| same_tys(cx, t, ty)) {
937-
span_lint(cx,
938-
NEW_RET_NO_SELF,
939-
implitem.span,
940-
"methods called `new` usually return `Self`");
934+
if let hir::ImplItemKind::Method(ref sig, id) = implitem.node {
935+
let ret_ty = return_ty(cx, implitem.id);
936+
if name == "new" &&
937+
!ret_ty.walk().any(|t| same_tys(cx, t, ty)) {
938+
span_lint(cx,
939+
NEW_RET_NO_SELF,
940+
implitem.span,
941+
"methods called `new` usually return `Self`");
942+
}
941943
}
942944
}
943945
}

tests/ui/new_ret_no_self.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,44 @@
55

66
fn main(){}
77

8-
//trait R {
9-
// type Item;
10-
//}
11-
//
12-
//struct S;
13-
//
14-
//impl R for S {
15-
// type Item = Self;
16-
//}
17-
//
18-
//impl S {
19-
// // should not trigger the lint
20-
// pub fn new() -> impl R<Item = Self> {
21-
// S
22-
// }
23-
//}
24-
//
25-
//struct S2;
26-
//
27-
//impl R for S2 {
28-
// type Item = Self;
29-
//}
30-
//
31-
//impl S2 {
32-
// // should not trigger the lint
33-
// pub fn new(_: String) -> impl R<Item = Self> {
34-
// S2
35-
// }
36-
//}
37-
//
38-
//struct T;
39-
//
40-
//impl T {
41-
// // should not trigger lint
42-
// pub fn new() -> Self {
43-
// unimplemented!();
44-
// }
45-
//}
8+
trait R {
9+
type Item;
10+
}
11+
12+
struct S;
13+
14+
impl R for S {
15+
type Item = Self;
16+
}
17+
18+
impl S {
19+
// should not trigger the lint
20+
pub fn new() -> impl R<Item = Self> {
21+
S
22+
}
23+
}
24+
25+
struct S2;
26+
27+
impl R for S2 {
28+
type Item = Self;
29+
}
30+
31+
impl S2 {
32+
// should not trigger the lint
33+
pub fn new(_: String) -> impl R<Item = Self> {
34+
S2
35+
}
36+
}
37+
38+
struct T;
39+
40+
impl T {
41+
// should not trigger lint
42+
pub fn new() -> Self {
43+
unimplemented!();
44+
}
45+
}
4646

4747
struct U;
4848

tests/ui/new_ret_no_self.stderr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: methods called `new` usually return `Self`
2+
--> $DIR/new_ret_no_self.rs:51:5
3+
|
4+
51 | / pub fn new() -> u32 {
5+
52 | | unimplemented!();
6+
53 | | }
7+
| |_____^
8+
9+
error: methods called `new` usually return `Self`
10+
--> $DIR/new_ret_no_self.rs:60:5
11+
|
12+
60 | / pub fn new(_: String) -> u32 {
13+
61 | | unimplemented!();
14+
62 | | }
15+
| |_____^
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)