new_ret_no_self added positive test cases

This commit is contained in:
Josh Mcguigan 2018-10-02 20:11:56 -07:00
parent 8b12eee112
commit eb854b233c
2 changed files with 74 additions and 11 deletions

View File

@ -878,6 +878,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
let name = implitem.ident.name;
let parent = cx.tcx.hir.get_parent(implitem.id);
let item = cx.tcx.hir.expect_item(parent);
let def_id = cx.tcx.hir.local_def_id(item.id);
let ty = cx.tcx.type_of(def_id);
if_chain! {
if let hir::ImplItemKind::Method(ref sig, id) = implitem.node;
if let Some(first_arg_ty) = sig.decl.inputs.get(0);
@ -899,8 +901,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
}
// check conventions w.r.t. conversion method names and predicates
let def_id = cx.tcx.hir.local_def_id(item.id);
let ty = cx.tcx.type_of(def_id);
let is_copy = is_copy(cx, ty);
for &(ref conv, self_kinds) in &CONVENTIONS {
if conv.check(&name.as_str()) {
@ -928,17 +928,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
break;
}
}
let ret_ty = return_ty(cx, implitem.id);
if name == "new" &&
!ret_ty.walk().any(|t| same_tys(cx, t, ty)) {
span_lint(cx,
NEW_RET_NO_SELF,
implitem.span,
"methods called `new` usually return `Self`");
}
}
}
let ret_ty = return_ty(cx, implitem.id);
if name == "new" &&
!ret_ty.walk().any(|t| same_tys(cx, t, ty)) {
span_lint(cx,
NEW_RET_NO_SELF,
implitem.span,
"methods called `new` usually return `Self`");
}
}
}

View File

@ -0,0 +1,63 @@
#![feature(tool_lints)]
#![warn(clippy::new_ret_no_self)]
#![allow(dead_code, clippy::trivially_copy_pass_by_ref)]
fn main(){}
//trait R {
// type Item;
//}
//
//struct S;
//
//impl R for S {
// type Item = Self;
//}
//
//impl S {
// // should not trigger the lint
// pub fn new() -> impl R<Item = Self> {
// S
// }
//}
//
//struct S2;
//
//impl R for S2 {
// type Item = Self;
//}
//
//impl S2 {
// // should not trigger the lint
// pub fn new(_: String) -> impl R<Item = Self> {
// S2
// }
//}
//
//struct T;
//
//impl T {
// // should not trigger lint
// pub fn new() -> Self {
// unimplemented!();
// }
//}
struct U;
impl U {
// should trigger lint
pub fn new() -> u32 {
unimplemented!();
}
}
struct V;
impl V {
// should trigger lint
pub fn new(_: String) -> u32 {
unimplemented!();
}
}