Common function to lint wrong self convention from impl and trait def
This commit is contained in:
parent
a6bb9276f7
commit
4af9382bec
@ -1674,32 +1674,14 @@ fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::Impl
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((ref conv, self_kinds)) = &CONVENTIONS
|
||||
.iter()
|
||||
.find(|(ref conv, _)| conv.check(&name))
|
||||
{
|
||||
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
|
||||
let lint = if item.vis.node.is_pub() {
|
||||
WRONG_PUB_SELF_CONVENTION
|
||||
} else {
|
||||
WRONG_SELF_CONVENTION
|
||||
};
|
||||
|
||||
span_lint(
|
||||
cx,
|
||||
lint,
|
||||
first_arg.pat.span,
|
||||
&format!("methods called `{}` usually take {}; consider choosing a less ambiguous name",
|
||||
conv,
|
||||
&self_kinds
|
||||
.iter()
|
||||
.map(|k| k.description())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" or ")
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
lint_wrong_self_convention(
|
||||
cx,
|
||||
&name,
|
||||
item.vis.node.is_pub(),
|
||||
self_ty,
|
||||
first_arg_ty,
|
||||
first_arg.pat.span
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1748,26 +1730,7 @@ fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>
|
||||
let self_ty = TraitRef::identity(cx.tcx, item.hir_id.owner.to_def_id()).self_ty();
|
||||
|
||||
then {
|
||||
if let Some((ref conv, self_kinds)) = &CONVENTIONS
|
||||
.iter()
|
||||
.find(|(ref conv, _)| conv.check(&item.ident.name.as_str()))
|
||||
{
|
||||
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
|
||||
span_lint(
|
||||
cx,
|
||||
WRONG_PUB_SELF_CONVENTION,
|
||||
first_arg_span,
|
||||
&format!("methods called `{}` usually take {}; consider choosing a less ambiguous name",
|
||||
conv,
|
||||
&self_kinds
|
||||
.iter()
|
||||
.map(|k| k.description())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" or ")
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
lint_wrong_self_convention(cx, &item.ident.name.as_str(), false, self_ty, first_arg_ty, first_arg_span);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1792,6 +1755,39 @@ fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>
|
||||
extract_msrv_attr!(LateContext);
|
||||
}
|
||||
|
||||
fn lint_wrong_self_convention<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
item_name: &str,
|
||||
is_pub: bool,
|
||||
self_ty: &'tcx TyS<'tcx>,
|
||||
first_arg_ty: &'tcx TyS<'tcx>,
|
||||
first_arg_span: Span,
|
||||
) {
|
||||
let lint = if is_pub {
|
||||
WRONG_PUB_SELF_CONVENTION
|
||||
} else {
|
||||
WRONG_SELF_CONVENTION
|
||||
};
|
||||
if let Some((ref conv, self_kinds)) = &CONVENTIONS.iter().find(|(ref conv, _)| conv.check(item_name)) {
|
||||
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
|
||||
span_lint(
|
||||
cx,
|
||||
lint,
|
||||
first_arg_span,
|
||||
&format!(
|
||||
"methods called `{}` usually take {}; consider choosing a less ambiguous name",
|
||||
conv,
|
||||
&self_kinds
|
||||
.iter()
|
||||
.map(|k| k.description())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" or ")
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks for the `OR_FUN_CALL` lint.
|
||||
#[allow(clippy::too_many_lines)]
|
||||
fn lint_or_fun_call<'tcx>(
|
||||
|
@ -90,7 +90,7 @@ pub async fn into_bar(self) -> Bar {
|
||||
}
|
||||
|
||||
// Lint also in trait definition (see #6307)
|
||||
mod issue6307{
|
||||
mod issue6307 {
|
||||
trait T: Sized {
|
||||
fn as_i32(self) {}
|
||||
fn as_u32(&self) {}
|
||||
@ -102,7 +102,7 @@ fn to_i32(self) {}
|
||||
fn to_u32(&self) {}
|
||||
fn from_i32(self) {}
|
||||
// check whether the lint can be allowed at the function level
|
||||
#[allow(clippy::wrong_pub_self_convention)]
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
fn from_cake(self) {}
|
||||
|
||||
// test for false positives
|
||||
@ -113,4 +113,4 @@ fn to_(self) {}
|
||||
fn from_(self) {}
|
||||
fn to_mut(&mut self) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -77,8 +77,6 @@ error: methods called `as_*` usually take self by reference or self by mutable r
|
||||
|
|
||||
LL | fn as_i32(self) {}
|
||||
| ^^^^
|
||||
|
|
||||
= note: `-D clippy::wrong-pub-self-convention` implied by `-D warnings`
|
||||
|
||||
error: methods called `into_*` usually take self by value; consider choosing a less ambiguous name
|
||||
--> $DIR/wrong_self_convention.rs:97:21
|
||||
|
Loading…
Reference in New Issue
Block a user