Skip to content

Commit 08ce8bb

Browse files
committed
Auto merge of rust-lang#7215 - ThibsG:WrongSelfFix7179, r=Manishearth
Trigger [`wrong_self_convention`] only if it has implicit self Lint [`wrong_self_convention`] only if the impl or trait has `self` _per sé_. Fixes: rust-lang#7179 changelog: trigger [`wrong_self_convention`] only if it has implicit self
2 parents 1fd9975 + cd241b3 commit 08ce8bb

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,16 +1838,18 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
18381838
}
18391839
}
18401840

1841-
wrong_self_convention::check(
1842-
cx,
1843-
&name,
1844-
item.vis.node.is_pub(),
1845-
self_ty,
1846-
first_arg_ty,
1847-
first_arg.pat.span,
1848-
implements_trait,
1849-
false
1850-
);
1841+
if sig.decl.implicit_self.has_implicit_self() {
1842+
wrong_self_convention::check(
1843+
cx,
1844+
&name,
1845+
item.vis.node.is_pub(),
1846+
self_ty,
1847+
first_arg_ty,
1848+
first_arg.pat.span,
1849+
implements_trait,
1850+
false
1851+
);
1852+
}
18511853
}
18521854
}
18531855

@@ -1903,7 +1905,9 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
19031905

19041906
if_chain! {
19051907
if let TraitItemKind::Fn(ref sig, _) = item.kind;
1908+
if sig.decl.implicit_self.has_implicit_self();
19061909
if let Some(first_arg_ty) = sig.decl.inputs.iter().next();
1910+
19071911
then {
19081912
let first_arg_span = first_arg_ty.span;
19091913
let first_arg_ty = hir_ty_to_ty(cx.tcx, first_arg_ty);

tests/ui/wrong_self_convention2.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,26 @@ mod issue7032 {
4242
}
4343
}
4444
}
45+
46+
mod issue7179 {
47+
pub struct S(i32);
48+
49+
impl S {
50+
// don't trigger (`s` is not `self`)
51+
pub fn from_be(s: Self) -> Self {
52+
S(i32::from_be(s.0))
53+
}
54+
55+
// lint
56+
pub fn from_be_self(self) -> Self {
57+
S(i32::from_be(self.0))
58+
}
59+
}
60+
61+
trait T {
62+
// don't trigger (`s` is not `self`)
63+
fn from_be(s: Self) -> Self;
64+
// lint
65+
fn from_be_self(self) -> Self;
66+
}
67+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: methods called `from_*` usually take no `self`
2+
--> $DIR/wrong_self_convention2.rs:56:29
3+
|
4+
LL | pub fn from_be_self(self) -> Self {
5+
| ^^^^
6+
|
7+
= note: `-D clippy::wrong-self-convention` implied by `-D warnings`
8+
= help: consider choosing a less ambiguous name
9+
10+
error: methods called `from_*` usually take no `self`
11+
--> $DIR/wrong_self_convention2.rs:65:25
12+
|
13+
LL | fn from_be_self(self) -> Self;
14+
| ^^^^
15+
|
16+
= help: consider choosing a less ambiguous name
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)