Skip to content

Commit 9323873

Browse files
committed
Common function to lint wrong self convention from impl and trait def
1 parent 7ccd6ac commit 9323873

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
@@ -1659,32 +1659,14 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
16591659
}
16601660
}
16611661

1662-
if let Some((ref conv, self_kinds)) = &CONVENTIONS
1663-
.iter()
1664-
.find(|(ref conv, _)| conv.check(&name))
1665-
{
1666-
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
1667-
let lint = if item.vis.node.is_pub() {
1668-
WRONG_PUB_SELF_CONVENTION
1669-
} else {
1670-
WRONG_SELF_CONVENTION
1671-
};
1672-
1673-
span_lint(
1674-
cx,
1675-
lint,
1676-
first_arg.pat.span,
1677-
&format!("methods called `{}` usually take {}; consider choosing a less ambiguous name",
1678-
conv,
1679-
&self_kinds
1680-
.iter()
1681-
.map(|k| k.description())
1682-
.collect::<Vec<_>>()
1683-
.join(" or ")
1684-
),
1685-
);
1686-
}
1687-
}
1662+
lint_wrong_self_convention(
1663+
cx,
1664+
&name,
1665+
item.vis.node.is_pub(),
1666+
self_ty,
1667+
first_arg_ty,
1668+
first_arg.pat.span
1669+
);
16881670
}
16891671
}
16901672

@@ -1733,26 +1715,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17331715
let self_ty = TraitRef::identity(cx.tcx, item.hir_id.owner.to_def_id()).self_ty();
17341716

17351717
then {
1736-
if let Some((ref conv, self_kinds)) = &CONVENTIONS
1737-
.iter()
1738-
.find(|(ref conv, _)| conv.check(&item.ident.name.as_str()))
1739-
{
1740-
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
1741-
span_lint(
1742-
cx,
1743-
WRONG_PUB_SELF_CONVENTION,
1744-
first_arg_span,
1745-
&format!("methods called `{}` usually take {}; consider choosing a less ambiguous name",
1746-
conv,
1747-
&self_kinds
1748-
.iter()
1749-
.map(|k| k.description())
1750-
.collect::<Vec<_>>()
1751-
.join(" or ")
1752-
),
1753-
);
1754-
}
1755-
}
1718+
lint_wrong_self_convention(cx, &item.ident.name.as_str(), false, self_ty, first_arg_ty, first_arg_span);
17561719
}
17571720
}
17581721

@@ -1775,6 +1738,39 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17751738
}
17761739
}
17771740

1741+
fn lint_wrong_self_convention<'tcx>(
1742+
cx: &LateContext<'tcx>,
1743+
item_name: &str,
1744+
is_pub: bool,
1745+
self_ty: &'tcx TyS<'tcx>,
1746+
first_arg_ty: &'tcx TyS<'tcx>,
1747+
first_arg_span: Span,
1748+
) {
1749+
let lint = if is_pub {
1750+
WRONG_PUB_SELF_CONVENTION
1751+
} else {
1752+
WRONG_SELF_CONVENTION
1753+
};
1754+
if let Some((ref conv, self_kinds)) = &CONVENTIONS.iter().find(|(ref conv, _)| conv.check(item_name)) {
1755+
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
1756+
span_lint(
1757+
cx,
1758+
lint,
1759+
first_arg_span,
1760+
&format!(
1761+
"methods called `{}` usually take {}; consider choosing a less ambiguous name",
1762+
conv,
1763+
&self_kinds
1764+
.iter()
1765+
.map(|k| k.description())
1766+
.collect::<Vec<_>>()
1767+
.join(" or ")
1768+
),
1769+
);
1770+
}
1771+
}
1772+
}
1773+
17781774
/// Checks for the `OR_FUN_CALL` lint.
17791775
#[allow(clippy::too_many_lines)]
17801776
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)