Auto merge of #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: #7179

changelog: trigger [`wrong_self_convention`] only if it has implicit self
This commit is contained in:
bors 2021-05-13 16:00:58 +00:00
commit 08ce8bb703
3 changed files with 56 additions and 10 deletions

View file

@ -1838,6 +1838,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
}
}
if sig.decl.implicit_self.has_implicit_self() {
wrong_self_convention::check(
cx,
&name,
@ -1850,6 +1851,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
);
}
}
}
// if this impl block implements a trait, lint in trait definition instead
if implements_trait {
@ -1903,7 +1905,9 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
if_chain! {
if let TraitItemKind::Fn(ref sig, _) = item.kind;
if sig.decl.implicit_self.has_implicit_self();
if let Some(first_arg_ty) = sig.decl.inputs.iter().next();
then {
let first_arg_span = first_arg_ty.span;
let first_arg_ty = hir_ty_to_ty(cx.tcx, first_arg_ty);

View file

@ -42,3 +42,26 @@ mod issue7032 {
}
}
}
mod issue7179 {
pub struct S(i32);
impl S {
// don't trigger (`s` is not `self`)
pub fn from_be(s: Self) -> Self {
S(i32::from_be(s.0))
}
// lint
pub fn from_be_self(self) -> Self {
S(i32::from_be(self.0))
}
}
trait T {
// don't trigger (`s` is not `self`)
fn from_be(s: Self) -> Self;
// lint
fn from_be_self(self) -> Self;
}
}

View file

@ -0,0 +1,19 @@
error: methods called `from_*` usually take no `self`
--> $DIR/wrong_self_convention2.rs:56:29
|
LL | pub fn from_be_self(self) -> Self {
| ^^^^
|
= note: `-D clippy::wrong-self-convention` implied by `-D warnings`
= help: consider choosing a less ambiguous name
error: methods called `from_*` usually take no `self`
--> $DIR/wrong_self_convention2.rs:65:25
|
LL | fn from_be_self(self) -> Self;
| ^^^^
|
= help: consider choosing a less ambiguous name
error: aborting due to 2 previous errors