new_ret_no_self corrected panic and added test stderr

This commit is contained in:
Josh Mcguigan 2018-10-03 03:55:31 -07:00
parent eb854b233c
commit 13ce96c4bf
3 changed files with 65 additions and 45 deletions

View file

@ -931,13 +931,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
} }
} }
let ret_ty = return_ty(cx, implitem.id); if let hir::ImplItemKind::Method(ref sig, id) = implitem.node {
if name == "new" && let ret_ty = return_ty(cx, implitem.id);
!ret_ty.walk().any(|t| same_tys(cx, t, ty)) { if name == "new" &&
span_lint(cx, !ret_ty.walk().any(|t| same_tys(cx, t, ty)) {
NEW_RET_NO_SELF, span_lint(cx,
implitem.span, NEW_RET_NO_SELF,
"methods called `new` usually return `Self`"); implitem.span,
"methods called `new` usually return `Self`");
}
} }
} }
} }

View file

@ -5,44 +5,44 @@
fn main(){} fn main(){}
//trait R { trait R {
// type Item; type Item;
//} }
//
//struct S; struct S;
//
//impl R for S { impl R for S {
// type Item = Self; type Item = Self;
//} }
//
//impl S { impl S {
// // should not trigger the lint // should not trigger the lint
// pub fn new() -> impl R<Item = Self> { pub fn new() -> impl R<Item = Self> {
// S S
// } }
//} }
//
//struct S2; struct S2;
//
//impl R for S2 { impl R for S2 {
// type Item = Self; type Item = Self;
//} }
//
//impl S2 { impl S2 {
// // should not trigger the lint // should not trigger the lint
// pub fn new(_: String) -> impl R<Item = Self> { pub fn new(_: String) -> impl R<Item = Self> {
// S2 S2
// } }
//} }
//
//struct T; struct T;
//
//impl T { impl T {
// // should not trigger lint // should not trigger lint
// pub fn new() -> Self { pub fn new() -> Self {
// unimplemented!(); unimplemented!();
// } }
//} }
struct U; struct U;

View file

@ -0,0 +1,18 @@
error: methods called `new` usually return `Self`
--> $DIR/new_ret_no_self.rs:51:5
|
51 | / pub fn new() -> u32 {
52 | | unimplemented!();
53 | | }
| |_____^
error: methods called `new` usually return `Self`
--> $DIR/new_ret_no_self.rs:60:5
|
60 | / pub fn new(_: String) -> u32 {
61 | | unimplemented!();
62 | | }
| |_____^
error: aborting due to 2 previous errors