Skip to content

Commit b713cd5

Browse files
committed
move is_in_trait_method to utils and rename
1 parent a43bfef commit b713cd5

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

clippy_lints/src/unused_async.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::is_def_id_trait_method;
23
use rustc_hir::intravisit::{walk_body, walk_expr, walk_fn, FnKind, Visitor};
3-
use rustc_hir::{Body, Expr, ExprKind, FnDecl, ItemKind, Node, YieldSource};
4+
use rustc_hir::{Body, Expr, ExprKind, FnDecl, YieldSource};
45
use rustc_lint::{LateContext, LateLintPass};
56
use rustc_middle::hir::nested_filter;
67
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -81,20 +82,6 @@ impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> {
8182
}
8283
}
8384

84-
/// Checks if the `def_id` belongs to a function and that function is part of a trait impl,
85-
/// in which case we shouldn't lint because `async` is part of the trait definition and therefore
86-
/// can't be removed.
87-
fn is_in_trait_impl(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
88-
if let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(def_id)
89-
&& let Node::Item(item) = cx.tcx.hir().get_parent(hir_id)
90-
&& let ItemKind::Impl(imp) = item.kind
91-
{
92-
imp.of_trait.is_some()
93-
} else {
94-
false
95-
}
96-
}
97-
9885
impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
9986
fn check_fn(
10087
&mut self,
@@ -105,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
10592
span: Span,
10693
def_id: LocalDefId,
10794
) {
108-
if !span.from_expansion() && fn_kind.asyncness().is_async() && !is_in_trait_impl(cx, def_id) {
95+
if !span.from_expansion() && fn_kind.asyncness().is_async() && !is_def_id_trait_method(cx, def_id) {
10996
let mut visitor = AsyncFnVisitor {
11097
cx,
11198
found_await: false,

clippy_utils/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,18 @@ pub fn is_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol)
327327
.map_or(false, |did| is_diag_trait_item(cx, did, diag_item))
328328
}
329329

330+
/// Checks if the `def_id` belongs to a function that is part of a trait impl.
331+
pub fn is_def_id_trait_method(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
332+
if let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(def_id)
333+
&& let Node::Item(item) = cx.tcx.hir().get_parent(hir_id)
334+
&& let ItemKind::Impl(imp) = item.kind
335+
{
336+
imp.of_trait.is_some()
337+
} else {
338+
false
339+
}
340+
}
341+
330342
/// Checks if the given expression is a path referring an item on the trait
331343
/// that is marked with the given diagnostic item.
332344
///

0 commit comments

Comments
 (0)