diff options
| author | Josh Mcguigan <joshmcg88@gmail.com> | 2018-10-03 03:55:31 -0700 |
|---|---|---|
| committer | Josh Mcguigan <joshmcg88@gmail.com> | 2018-10-13 06:20:39 -0700 |
| commit | 13ce96c4bfd236ec49bcd7b63f42f9a51b0ee599 (patch) | |
| tree | 58032b03d81c2e06fb7485fb5b83d78796df4c7b | |
| parent | eb854b233c353441f86c4a346a941c5965a2333a (diff) | |
| download | rust-13ce96c4bfd236ec49bcd7b63f42f9a51b0ee599.tar.gz rust-13ce96c4bfd236ec49bcd7b63f42f9a51b0ee599.zip | |
new_ret_no_self corrected panic and added test stderr
| -rw-r--r-- | clippy_lints/src/methods/mod.rs | 16 | ||||
| -rw-r--r-- | tests/ui/new_ret_no_self.rs | 76 | ||||
| -rw-r--r-- | tests/ui/new_ret_no_self.stderr | 18 |
3 files changed, 65 insertions, 45 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index d11dbf0e773..81cb1cd1182 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -931,13 +931,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { } } - 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`"); + if let hir::ImplItemKind::Method(ref sig, id) = implitem.node { + 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`"); + } } } } diff --git a/tests/ui/new_ret_no_self.rs b/tests/ui/new_ret_no_self.rs index 67933f00262..762dd582168 100644 --- a/tests/ui/new_ret_no_self.rs +++ b/tests/ui/new_ret_no_self.rs @@ -5,44 +5,44 @@ 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!(); -// } -//} +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; diff --git a/tests/ui/new_ret_no_self.stderr b/tests/ui/new_ret_no_self.stderr new file mode 100644 index 00000000000..1d698892449 --- /dev/null +++ b/tests/ui/new_ret_no_self.stderr @@ -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 + |
