Skip to content

Commit 60e6b70

Browse files
committed
Refactor: Extract trait_ref_of_method function
1 parent 00baf7a commit 60e6b70

File tree

4 files changed

+21
-27
lines changed

4 files changed

+21
-27
lines changed

clippy_lints/src/assign_ops.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, SpanlessEq};
1+
use crate::utils::{get_trait_def_id, implements_trait, snippet_opt, span_lint_and_then, trait_ref_of_method, SpanlessEq};
22
use crate::utils::{higher, sugg};
33
use if_chain::if_chain;
44
use rustc::hir;
@@ -140,13 +140,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
140140
};
141141
// check that we are not inside an `impl AssignOp` of this exact operation
142142
let parent_fn = cx.tcx.hir().get_parent_item(e.hir_id);
143-
let parent_impl = cx.tcx.hir().get_parent_item(parent_fn);
144-
// the crate node is the only one that is not in the map
145143
if_chain! {
146-
if parent_impl != hir::CRATE_HIR_ID;
147-
if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl);
148-
if let hir::ItemKind::Impl(_, _, _, _, Some(trait_ref), _, _) =
149-
&item.node;
144+
if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
150145
if trait_ref.path.def.def_id() == trait_id;
151146
then { return; }
152147
}

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::utils::{is_entrypoint_fn, span_lint};
2-
use if_chain::if_chain;
1+
use crate::utils::{is_entrypoint_fn, span_lint, trait_ref_of_method};
32
use rustc::hir;
43
use rustc::hir::intravisit::FnKind;
54
use rustc::hir::{Body, Constness, FnDecl, HirId};
@@ -96,7 +95,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
9695
}
9796
},
9897
FnKind::Method(_, sig, ..) => {
99-
if is_trait_method(cx, hir_id) || already_const(sig.header) {
98+
if trait_ref_of_method(cx, hir_id).is_some() || already_const(sig.header) {
10099
return;
101100
}
102101
},
@@ -115,18 +114,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn {
115114
}
116115
}
117116

118-
fn is_trait_method(cx: &LateContext<'_, '_>, hir_id: HirId) -> bool {
119-
// Get the implemented trait for the current function
120-
let parent_impl = cx.tcx.hir().get_parent_item(hir_id);
121-
if_chain! {
122-
if parent_impl != hir::CRATE_HIR_ID;
123-
if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl);
124-
if let hir::ItemKind::Impl(_, _, _, _, Some(_trait_ref), _, _) = &item.node;
125-
then { return true; }
126-
}
127-
false
128-
}
129-
130117
// We don't have to lint on something that's already `const`
131118
fn already_const(header: hir::FnHeader) -> bool {
132119
header.constness == Constness::Const

clippy_lints/src/suspicious_trait_impl.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{get_trait_def_id, span_lint};
1+
use crate::utils::{get_trait_def_id, span_lint, trait_ref_of_method};
22
use if_chain::if_chain;
33
use rustc::hir;
44
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
@@ -177,12 +177,9 @@ fn check_binop<'a>(
177177

178178
// Get the actually implemented trait
179179
let parent_fn = cx.tcx.hir().get_parent_item(expr.hir_id);
180-
let parent_impl = cx.tcx.hir().get_parent_item(parent_fn);
181180

182181
if_chain! {
183-
if parent_impl != hir::CRATE_HIR_ID;
184-
if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl);
185-
if let hir::ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) = item.node;
182+
if let Some(trait_ref) = trait_ref_of_method(cx, parent_fn);
186183
if let Some(idx) = trait_ids.iter().position(|&tid| tid == trait_ref.path.def.def_id());
187184
if binop != expected_ops[idx];
188185
then{

clippy_lints/src/utils/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,21 @@ pub fn get_trait_def_id(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<DefId
279279
}
280280
}
281281

282+
/// Get the `hir::TraitRef` of the trait the given method is implemented for
283+
///
284+
/// See https://doc.rust-lang.org/nightly/nightly-rustc/rustc/hir/struct.TraitRef.html
285+
pub fn trait_ref_of_method(cx: &LateContext<'_, '_>, hir_id: HirId) -> Option<TraitRef> {
286+
// Get the implemented trait for the current function
287+
let parent_impl = cx.tcx.hir().get_parent_item(hir_id);
288+
if_chain! {
289+
if parent_impl != hir::CRATE_HIR_ID;
290+
if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl);
291+
if let hir::ItemKind::Impl(_, _, _, _, trait_ref, _, _) = &item.node;
292+
then { return trait_ref.clone(); }
293+
}
294+
None
295+
}
296+
282297
/// Check whether a type implements a trait.
283298
/// See also `get_trait_def_id`.
284299
pub fn implements_trait<'a, 'tcx>(

0 commit comments

Comments
 (0)