Skip to content

Commit 1436fea

Browse files
committed
Refactor 'check_impl_item'
1 parent 7a11843 commit 1436fea

File tree

1 file changed

+48
-49
lines changed
  • clippy_lints/src/methods

1 file changed

+48
-49
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,56 +1075,57 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
10751075
if let hir::ImplItemKind::Method(ref sig, id) = impl_item.node;
10761076
if let Some(first_arg) = iter_input_pats(&sig.decl, cx.tcx.hir().body(id)).next();
10771077
if let hir::ItemKind::Impl(_, _, _, _, None, _, _) = item.node;
1078+
1079+
let method_def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
1080+
let method_sig = cx.tcx.fn_sig(method_def_id);
1081+
let method_sig = cx.tcx.erase_late_bound_regions(&method_sig);
1082+
1083+
let first_arg_ty = &method_sig.inputs().iter().next();
1084+
1085+
// check conventions w.r.t. conversion method names and predicates
1086+
if let Some(first_arg_ty) = first_arg_ty;
1087+
10781088
then {
1079-
let method_def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
1080-
let method_sig = cx.tcx.fn_sig(method_def_id);
1081-
let method_sig = cx.tcx.erase_late_bound_regions(&method_sig);
1082-
1083-
let first_arg_ty = &method_sig.inputs().iter().next();
1084-
1085-
// check conventions w.r.t. conversion method names and predicates
1086-
if let Some(first_arg_ty) = first_arg_ty {
1087-
1088-
if cx.access_levels.is_exported(impl_item.hir_id) {
1089-
// check missing trait implementations
1090-
for &(method_name, n_args, self_kind, out_type, trait_name) in &TRAIT_METHODS {
1091-
if name == method_name &&
1092-
sig.decl.inputs.len() == n_args &&
1093-
out_type.matches(cx, &sig.decl.output) &&
1094-
self_kind.matches(cx, ty, first_arg_ty) {
1095-
span_lint(cx, SHOULD_IMPLEMENT_TRAIT, impl_item.span, &format!(
1096-
"defining a method called `{}` on this type; consider implementing \
1097-
the `{}` trait or choosing a less ambiguous name", name, trait_name));
1098-
}
1089+
if cx.access_levels.is_exported(impl_item.hir_id) {
1090+
// check missing trait implementations
1091+
for &(method_name, n_args, self_kind, out_type, trait_name) in &TRAIT_METHODS {
1092+
if name == method_name &&
1093+
sig.decl.inputs.len() == n_args &&
1094+
out_type.matches(cx, &sig.decl.output) &&
1095+
self_kind.matches(cx, ty, first_arg_ty) {
1096+
span_lint(cx, SHOULD_IMPLEMENT_TRAIT, impl_item.span, &format!(
1097+
"defining a method called `{}` on this type; consider implementing \
1098+
the `{}` trait or choosing a less ambiguous name", name, trait_name));
10991099
}
11001100
}
1101+
}
11011102

1102-
for &(ref conv, self_kinds) in &CONVENTIONS {
1103-
if conv.check(&name) {
1104-
if !self_kinds
1105-
.iter()
1106-
.any(|k| k.matches(cx, ty, first_arg_ty)) {
1107-
let lint = if item.vis.node.is_pub() {
1108-
WRONG_PUB_SELF_CONVENTION
1109-
} else {
1110-
WRONG_SELF_CONVENTION
1111-
};
1112-
span_lint(cx,
1113-
lint,
1114-
first_arg.pat.span,
1115-
&format!("methods called `{}` usually take {}; consider choosing a less \
1116-
ambiguous name",
1117-
conv,
1118-
&self_kinds.iter()
1119-
.map(|k| k.description())
1120-
.collect::<Vec<_>>()
1121-
.join(" or ")));
1122-
}
1103+
if let Some((ref conv, self_kinds)) = &CONVENTIONS
1104+
.iter()
1105+
.find(|(ref conv, _)| conv.check(&name))
1106+
{
1107+
if !self_kinds.iter().any(|k| k.matches(cx, ty, first_arg_ty)) {
1108+
let lint = if item.vis.node.is_pub() {
1109+
WRONG_PUB_SELF_CONVENTION
1110+
} else {
1111+
WRONG_SELF_CONVENTION
1112+
};
11231113

1124-
// Only check the first convention to match (CONVENTIONS should be listed from most to least
1125-
// specific)
1126-
break;
1127-
}
1114+
span_lint(
1115+
cx,
1116+
lint,
1117+
first_arg.pat.span,
1118+
&format!(
1119+
"methods called `{}` usually take {}; consider choosing a less \
1120+
ambiguous name",
1121+
conv,
1122+
&self_kinds
1123+
.iter()
1124+
.map(|k| k.description())
1125+
.collect::<Vec<_>>()
1126+
.join(" or ")
1127+
),
1128+
);
11281129
}
11291130
}
11301131
}
@@ -1134,10 +1135,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
11341135
let ret_ty = return_ty(cx, impl_item.hir_id);
11351136

11361137
// walk the return type and check for Self (this does not check associated types)
1137-
for inner_type in ret_ty.walk() {
1138-
if same_tys(cx, ty, inner_type) {
1139-
return;
1140-
}
1138+
if ret_ty.walk().any(|inner_type| same_tys(cx, ty, inner_type)) {
1139+
return;
11411140
}
11421141

11431142
// if return type is impl trait, check the associated types

0 commit comments

Comments
 (0)