Skip to content

Commit 7cc863b

Browse files
author
Anthony Huang
committed
Methods with self are not constructors
1 parent cbb95c8 commit 7cc863b

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

clippy_lints/src/self_named_constructor.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ declare_lint_pass!(SelfNamedConstructor => [SELF_NAMED_CONSTRUCTOR]);
4343
impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructor {
4444
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
4545
match impl_item.kind {
46-
ImplItemKind::Fn(_, _) => {},
46+
ImplItemKind::Fn(ref sig, _) => {
47+
if sig.decl.implicit_self.has_implicit_self() {
48+
return;
49+
}
50+
},
4751
_ => return,
4852
}
4953

tests/ui/missing_const_for_fn/could_be_const.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mod with_drop {
6565

6666
impl B {
6767
// This can be const, because `a` is passed by reference
68-
pub fn new(a: &A) -> B {
68+
pub fn b(self, a: &A) -> B {
6969
B
7070
}
7171
}

tests/ui/self_named_constructor.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,12 @@ impl TraitSameTypeName for ShouldNotSpawn {
4848
}
4949
}
5050

51+
struct SelfMethodShouldNotSpawn;
52+
53+
impl SelfMethodShouldNotSpawn {
54+
fn self_method_should_not_spawn(self) -> Self {
55+
SelfMethodShouldNotSpawn
56+
}
57+
}
58+
5159
fn main() {}

0 commit comments

Comments
 (0)