Skip to content

Commit d409b5c

Browse files
committed
extra_unused_type_parameters:
* Inline `is_empty_exported_or_macro`. * Check if there are any generic parameters before walking the signature.
1 parent f178316 commit d409b5c

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

clippy_lints/src/extra_unused_type_parameters.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,6 @@ impl ExtraUnusedTypeParameters {
5050
avoid_breaking_exported_api,
5151
}
5252
}
53-
54-
/// Don't lint external macros or functions with empty bodies. Also, don't lint exported items
55-
/// if the `avoid_breaking_exported_api` config option is set.
56-
fn is_empty_exported_or_macro(
57-
&self,
58-
cx: &LateContext<'_>,
59-
span: Span,
60-
def_id: LocalDefId,
61-
body_id: BodyId,
62-
) -> bool {
63-
let body = cx.tcx.hir().body(body_id).value;
64-
let fn_empty = matches!(&body.kind, ExprKind::Block(blk, None) if blk.stmts.is_empty() && blk.expr.is_none());
65-
let is_exported = cx.effective_visibilities.is_exported(def_id);
66-
in_external_macro(cx.sess(), span) || fn_empty || (is_exported && self.avoid_breaking_exported_api)
67-
}
6853
}
6954

7055
impl_lint_pass!(ExtraUnusedTypeParameters => [EXTRA_UNUSED_TYPE_PARAMETERS]);
@@ -266,10 +251,17 @@ impl<'cx, 'tcx> Visitor<'tcx> for TypeWalker<'cx, 'tcx> {
266251
}
267252
}
268253

254+
fn is_empty_body(cx: &LateContext<'_>, body: BodyId) -> bool {
255+
matches!(cx.tcx.hir().body(body).value.kind, ExprKind::Block(b, _) if b.stmts.is_empty() && b.expr.is_none())
256+
}
257+
269258
impl<'tcx> LateLintPass<'tcx> for ExtraUnusedTypeParameters {
270259
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
271260
if let ItemKind::Fn(_, generics, body_id) = item.kind
272-
&& !self.is_empty_exported_or_macro(cx, item.span, item.owner_id.def_id, body_id)
261+
&& !generics.params.is_empty()
262+
&& !is_empty_body(cx, body_id)
263+
&& (!self.avoid_breaking_exported_api || !cx.effective_visibilities.is_exported(item.owner_id.def_id))
264+
&& !in_external_macro(cx.sess(), item.span)
273265
&& !is_from_proc_macro(cx, item)
274266
{
275267
let mut walker = TypeWalker::new(cx, generics);
@@ -281,8 +273,12 @@ impl<'tcx> LateLintPass<'tcx> for ExtraUnusedTypeParameters {
281273
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) {
282274
// Only lint on inherent methods, not trait methods.
283275
if let ImplItemKind::Fn(.., body_id) = item.kind
276+
&& !item.generics.params.is_empty()
284277
&& trait_ref_of_method(cx, item.owner_id.def_id).is_none()
285-
&& !self.is_empty_exported_or_macro(cx, item.span, item.owner_id.def_id, body_id)
278+
&& !is_empty_body(cx, body_id)
279+
&& (!self.avoid_breaking_exported_api || !cx.effective_visibilities.is_exported(item.owner_id.def_id))
280+
&& !in_external_macro(cx.sess(), item.span)
281+
&& !is_from_proc_macro(cx, item)
286282
{
287283
let mut walker = TypeWalker::new(cx, item.generics);
288284
walk_impl_item(&mut walker, item);

0 commit comments

Comments
 (0)