Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4af9382

Browse files
committed
Common function to lint wrong self convention from impl and trait def
1 parent a6bb927 commit 4af9382

File tree

3 files changed

+45
-51
lines changed

3 files changed

+45
-51
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,32 +1674,14 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16741674
}
16751675
}
16761676

1677-
if let Some((ref conv, self_kinds)) = &CONVENTIONS
1678-
.iter()
1679-
.find(|(ref conv, _)| conv.check(&name))
1680-
{
1681-
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
1682-
let lint = if item.vis.node.is_pub() {
1683-
WRONG_PUB_SELF_CONVENTION
1684-
} else {
1685-
WRONG_SELF_CONVENTION
1686-
};
1687-
1688-
span_lint(
1689-
cx,
1690-
lint,
1691-
first_arg.pat.span,
1692-
&format!("methods called `{}` usually take {}; consider choosing a less ambiguous name",
1693-
conv,
1694-
&self_kinds
1695-
.iter()
1696-
.map(|k| k.description())
1697-
.collect::<Vec<_>>()
1698-
.join(" or ")
1699-
),
1700-
);
1701-
}
1702-
}
1677+
lint_wrong_self_convention(
1678+
cx,
1679+
&name,
1680+
item.vis.node.is_pub(),
1681+
self_ty,
1682+
first_arg_ty,
1683+
first_arg.pat.span
1684+
);
17031685
}
17041686
}
17051687

@@ -1748,26 +1730,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17481730
let self_ty = TraitRef::identity(cx.tcx, item.hir_id.owner.to_def_id()).self_ty();
17491731

17501732
then {
1751-
if let Some((ref conv, self_kinds)) = &CONVENTIONS
1752-
.iter()
1753-
.find(|(ref conv, _)| conv.check(&item.ident.name.as_str()))
1754-
{
1755-
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
1756-
span_lint(
1757-
cx,
1758-
WRONG_PUB_SELF_CONVENTION,
1759-
first_arg_span,
1760-
&format!("methods called `{}` usually take {}; consider choosing a less ambiguous name",
1761-
conv,
1762-
&self_kinds
1763-
.iter()
1764-
.map(|k| k.description())
1765-
.collect::<Vec<_>>()
1766-
.join(" or ")
1767-
),
1768-
);
1769-
}
1770-
}
1733+
lint_wrong_self_convention(cx, &item.ident.name.as_str(), false, self_ty, first_arg_ty, first_arg_span);
17711734
}
17721735
}
17731736

@@ -1792,6 +1755,39 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17921755
extract_msrv_attr!(LateContext);
17931756
}
17941757

1758+
fn lint_wrong_self_convention<'tcx>(
1759+
cx: &LateContext<'tcx>,
1760+
item_name: &str,
1761+
is_pub: bool,
1762+
self_ty: &'tcx TyS<'tcx>,
1763+
first_arg_ty: &'tcx TyS<'tcx>,
1764+
first_arg_span: Span,
1765+
) {
1766+
let lint = if is_pub {
1767+
WRONG_PUB_SELF_CONVENTION
1768+
} else {
1769+
WRONG_SELF_CONVENTION
1770+
};
1771+
if let Some((ref conv, self_kinds)) = &CONVENTIONS.iter().find(|(ref conv, _)| conv.check(item_name)) {
1772+
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
1773+
span_lint(
1774+
cx,
1775+
lint,
1776+
first_arg_span,
1777+
&format!(
1778+
"methods called `{}` usually take {}; consider choosing a less ambiguous name",
1779+
conv,
1780+
&self_kinds
1781+
.iter()
1782+
.map(|k| k.description())
1783+
.collect::<Vec<_>>()
1784+
.join(" or ")
1785+
),
1786+
);
1787+
}
1788+
}
1789+
}
1790+
17951791
/// Checks for the `OR_FUN_CALL` lint.
17961792
#[allow(clippy::too_many_lines)]
17971793
fn lint_or_fun_call<'tcx>(

tests/ui/wrong_self_convention.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ mod issue4037 {
9090
}
9191

9292
// Lint also in trait definition (see #6307)
93-
mod issue6307{
93+
mod issue6307 {
9494
trait T: Sized {
9595
fn as_i32(self) {}
9696
fn as_u32(&self) {}
@@ -102,7 +102,7 @@ mod issue6307{
102102
fn to_u32(&self) {}
103103
fn from_i32(self) {}
104104
// check whether the lint can be allowed at the function level
105-
#[allow(clippy::wrong_pub_self_convention)]
105+
#[allow(clippy::wrong_self_convention)]
106106
fn from_cake(self) {}
107107

108108
// test for false positives
@@ -113,4 +113,4 @@ mod issue6307{
113113
fn from_(self) {}
114114
fn to_mut(&mut self) {}
115115
}
116-
}
116+
}

tests/ui/wrong_self_convention.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ error: methods called `as_*` usually take self by reference or self by mutable r
7777
|
7878
LL | fn as_i32(self) {}
7979
| ^^^^
80-
|
81-
= note: `-D clippy::wrong-pub-self-convention` implied by `-D warnings`
8280

8381
error: methods called `into_*` usually take self by value; consider choosing a less ambiguous name
8482
--> $DIR/wrong_self_convention.rs:97:21

0 commit comments

Comments
 (0)