Skip to content

Commit a25c195

Browse files
committed
Remove redundant work in trait_ref_of_method.
1 parent fc811f7 commit a25c195

File tree

10 files changed

+12
-16
lines changed

10 files changed

+12
-16
lines changed

clippy_lints/src/extra_unused_type_parameters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl<'tcx> LateLintPass<'tcx> for ExtraUnusedTypeParameters {
273273
// Only lint on inherent methods, not trait methods.
274274
if let ImplItemKind::Fn(.., body_id) = item.kind
275275
&& !item.generics.params.is_empty()
276-
&& trait_ref_of_method(cx, item.owner_id.def_id).is_none()
276+
&& trait_ref_of_method(cx, item.owner_id).is_none()
277277
&& !is_empty_body(cx, body_id)
278278
&& (!self.avoid_breaking_exported_api || !cx.effective_visibilities.is_exported(item.owner_id.def_id))
279279
&& !item.span.in_external_macro(cx.sess().source_map())

clippy_lints/src/functions/must_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Imp
5555
let attr = cx.tcx.get_attr(item.owner_id, sym::must_use);
5656
if let Some(attr) = attr {
5757
check_needless_must_use(cx, sig.decl, item.owner_id, item.span, fn_header_span, attr, attrs, sig);
58-
} else if is_public && !is_proc_macro(attrs) && trait_ref_of_method(cx, item.owner_id.def_id).is_none() {
58+
} else if is_public && !is_proc_macro(attrs) && trait_ref_of_method(cx, item.owner_id).is_none() {
5959
check_must_use_candidate(
6060
cx,
6161
sig.decl,

clippy_lints/src/functions/result.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub(super) fn check_impl_item<'tcx>(
5555
// Don't lint if method is a trait's implementation, we can't do anything about those
5656
if let hir::ImplItemKind::Fn(ref sig, _) = item.kind
5757
&& let Some((hir_ty, err_ty)) = result_err_ty(cx, sig.decl, item.owner_id.def_id, item.span)
58-
&& trait_ref_of_method(cx, item.owner_id.def_id).is_none()
58+
&& trait_ref_of_method(cx, item.owner_id).is_none()
5959
{
6060
if cx.effective_visibilities.is_exported(item.owner_id.def_id) {
6161
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());

clippy_lints/src/inherent_to_string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl<'tcx> LateLintPass<'tcx> for InherentToString {
106106
// Check if return type is String
107107
&& is_type_lang_item(cx, return_ty(cx, impl_item.owner_id), LangItem::String)
108108
// Filters instances of to_string which are required by a trait
109-
&& trait_ref_of_method(cx, impl_item.owner_id.def_id).is_none()
109+
&& trait_ref_of_method(cx, impl_item.owner_id).is_none()
110110
{
111111
show_lint(cx, impl_item);
112112
}

clippy_lints/src/lifetimes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for Lifetimes {
159159

160160
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
161161
if let ImplItemKind::Fn(ref sig, id) = item.kind {
162-
let report_extra_lifetimes = trait_ref_of_method(cx, item.owner_id.def_id).is_none();
162+
let report_extra_lifetimes = trait_ref_of_method(cx, item.owner_id).is_none();
163163
check_fn_inner(
164164
cx,
165165
sig,

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_abi::ExternAbi;
77
use rustc_errors::Applicability;
88
use rustc_hir::def_id::CRATE_DEF_ID;
99
use rustc_hir::intravisit::FnKind;
10-
use rustc_hir::{self as hir, Body, Constness, FnDecl, GenericParamKind};
10+
use rustc_hir::{self as hir, Body, Constness, FnDecl, GenericParamKind, OwnerId};
1111
use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_middle::ty;
1313
use rustc_session::impl_lint_pass;
@@ -125,7 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
125125
}
126126
},
127127
FnKind::Method(_, sig, ..) => {
128-
if already_const(sig.header) || trait_ref_of_method(cx, def_id).is_some() {
128+
if already_const(sig.header) || trait_ref_of_method(cx, OwnerId { def_id }).is_some() {
129129
return;
130130
}
131131
},

clippy_lints/src/mut_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for MutableKeyType<'tcx> {
8383

8484
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'tcx>) {
8585
if let hir::ImplItemKind::Fn(ref sig, ..) = item.kind
86-
&& trait_ref_of_method(cx, item.owner_id.def_id).is_none()
86+
&& trait_ref_of_method(cx, item.owner_id).is_none()
8787
{
8888
self.check_sig(cx, item.owner_id.def_id, sig.decl);
8989
}

clippy_lints/src/operators/assign_op_pattern.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub(super) fn check<'tcx>(
2626
let rty = cx.typeck_results().expr_ty(rhs);
2727
if let Some((_, lang_item)) = binop_traits(op.node)
2828
&& let Some(trait_id) = cx.tcx.lang_items().get(lang_item)
29-
&& let parent_fn = cx.tcx.hir_get_parent_item(e.hir_id).def_id
29+
&& let parent_fn = cx.tcx.hir_get_parent_item(e.hir_id)
3030
&& trait_ref_of_method(cx, parent_fn).is_none_or(|t| t.path.res.def_id() != trait_id)
3131
&& implements_trait(cx, ty, trait_id, &[rty.into()])
3232
{

clippy_lints/src/suspicious_trait_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn check_expr_inner<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, bin
8080
&& let hir::Node::ImplItem(impl_item) = cx.tcx.hir_node_by_def_id(parent_fn)
8181
&& let hir::ImplItemKind::Fn(_, body_id) = impl_item.kind
8282
&& let body = cx.tcx.hir_body(body_id)
83-
&& let parent_fn = cx.tcx.hir_get_parent_item(expr.hir_id).def_id
83+
&& let parent_fn = cx.tcx.hir_get_parent_item(expr.hir_id)
8484
&& let Some(trait_ref) = trait_ref_of_method(cx, parent_fn)
8585
&& let trait_id = trait_ref.path.res.def_id()
8686
&& ![binop_trait_id, op_assign_trait_id].contains(&trait_id)

clippy_utils/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,8 @@ pub fn path_def_id<'tcx>(cx: &LateContext<'_>, maybe_path: &impl MaybePath<'tcx>
523523
/// }
524524
/// }
525525
/// ```
526-
pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, def_id: LocalDefId) -> Option<&'tcx TraitRef<'tcx>> {
527-
// Get the implemented trait for the current function
528-
let hir_id = cx.tcx.local_def_id_to_hir_id(def_id);
529-
let parent_impl = cx.tcx.hir_get_parent_item(hir_id);
530-
if parent_impl != hir::CRATE_OWNER_ID
531-
&& let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent_impl.def_id)
526+
pub fn trait_ref_of_method<'tcx>(cx: &LateContext<'tcx>, owner: OwnerId) -> Option<&'tcx TraitRef<'tcx>> {
527+
if let Node::Item(item) = cx.tcx.hir_node(cx.tcx.hir_owner_parent(owner))
532528
&& let ItemKind::Impl(impl_) = &item.kind
533529
{
534530
return impl_.of_trait.as_ref();

0 commit comments

Comments
 (0)