Skip to content

Commit eb854b2

Browse files
committed
new_ret_no_self added positive test cases
1 parent 8b12eee commit eb854b2

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
878878
let name = implitem.ident.name;
879879
let parent = cx.tcx.hir.get_parent(implitem.id);
880880
let item = cx.tcx.hir.expect_item(parent);
881+
let def_id = cx.tcx.hir.local_def_id(item.id);
882+
let ty = cx.tcx.type_of(def_id);
881883
if_chain! {
882884
if let hir::ImplItemKind::Method(ref sig, id) = implitem.node;
883885
if let Some(first_arg_ty) = sig.decl.inputs.get(0);
@@ -899,8 +901,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
899901
}
900902

901903
// check conventions w.r.t. conversion method names and predicates
902-
let def_id = cx.tcx.hir.local_def_id(item.id);
903-
let ty = cx.tcx.type_of(def_id);
904904
let is_copy = is_copy(cx, ty);
905905
for &(ref conv, self_kinds) in &CONVENTIONS {
906906
if conv.check(&name.as_str()) {
@@ -928,17 +928,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
928928
break;
929929
}
930930
}
931-
932-
let ret_ty = return_ty(cx, implitem.id);
933-
if name == "new" &&
934-
!ret_ty.walk().any(|t| same_tys(cx, t, ty)) {
935-
span_lint(cx,
936-
NEW_RET_NO_SELF,
937-
implitem.span,
938-
"methods called `new` usually return `Self`");
939-
}
940931
}
941932
}
933+
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`");
941+
}
942942
}
943943
}
944944

tests/ui/new_ret_no_self.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#![feature(tool_lints)]
2+
3+
#![warn(clippy::new_ret_no_self)]
4+
#![allow(dead_code, clippy::trivially_copy_pass_by_ref)]
5+
6+
fn main(){}
7+
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+
//}
46+
47+
struct U;
48+
49+
impl U {
50+
// should trigger lint
51+
pub fn new() -> u32 {
52+
unimplemented!();
53+
}
54+
}
55+
56+
struct V;
57+
58+
impl V {
59+
// should trigger lint
60+
pub fn new(_: String) -> u32 {
61+
unimplemented!();
62+
}
63+
}

0 commit comments

Comments
 (0)