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);
}
// 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()) {
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`");
+ }
}
}
--- /dev/null
+#![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!();
+ }
+}