Skip to content

Commit

Permalink
move is_in_trait_method to utils and rename
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Jun 28, 2023
1 parent a43bfef commit b713cd5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
19 changes: 3 additions & 16 deletions clippy_lints/src/unused_async.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_def_id_trait_method;
use rustc_hir::intravisit::{walk_body, walk_expr, walk_fn, FnKind, Visitor};
use rustc_hir::{Body, Expr, ExprKind, FnDecl, ItemKind, Node, YieldSource};
use rustc_hir::{Body, Expr, ExprKind, FnDecl, YieldSource};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter;
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -81,20 +82,6 @@ impl<'a, 'tcx> Visitor<'tcx> for AsyncFnVisitor<'a, 'tcx> {
}
}

/// Checks if the `def_id` belongs to a function and that function is part of a trait impl,
/// in which case we shouldn't lint because `async` is part of the trait definition and therefore
/// can't be removed.
fn is_in_trait_impl(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
if let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(def_id)
&& let Node::Item(item) = cx.tcx.hir().get_parent(hir_id)
&& let ItemKind::Impl(imp) = item.kind
{
imp.of_trait.is_some()
} else {
false
}
}

impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
fn check_fn(
&mut self,
Expand All @@ -105,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
span: Span,
def_id: LocalDefId,
) {
if !span.from_expansion() && fn_kind.asyncness().is_async() && !is_in_trait_impl(cx, def_id) {
if !span.from_expansion() && fn_kind.asyncness().is_async() && !is_def_id_trait_method(cx, def_id) {
let mut visitor = AsyncFnVisitor {
cx,
found_await: false,
Expand Down
12 changes: 12 additions & 0 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ pub fn is_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol)
.map_or(false, |did| is_diag_trait_item(cx, did, diag_item))
}

/// Checks if the `def_id` belongs to a function that is part of a trait impl.
pub fn is_def_id_trait_method(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
if let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(def_id)
&& let Node::Item(item) = cx.tcx.hir().get_parent(hir_id)
&& let ItemKind::Impl(imp) = item.kind
{
imp.of_trait.is_some()
} else {
false
}
}

/// Checks if the given expression is a path referring an item on the trait
/// that is marked with the given diagnostic item.
///
Expand Down

0 comments on commit b713cd5

Please sign in to comment.