Skip to content

Commit 3771fe4

Browse files
committed
Factor out expr_path_res
1 parent 98c6381 commit 3771fe4

File tree

4 files changed

+11
-24
lines changed

4 files changed

+11
-24
lines changed

clippy_lints/src/misc.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use rustc_span::symbol::sym;
2020
use clippy_utils::consts::{constant, Constant};
2121
use clippy_utils::sugg::Sugg;
2222
use clippy_utils::{
23-
expr_path_res, get_item_name, get_parent_expr, in_constant, is_diag_trait_item, is_integer_const, iter_input_pats,
24-
last_path_segment, match_any_def_paths, paths, unsext, SpanlessEq,
23+
get_item_name, get_parent_expr, in_constant, is_diag_trait_item, is_integer_const, iter_input_pats,
24+
last_path_segment, match_any_def_paths, path_def_id, paths, unsext, SpanlessEq,
2525
};
2626

2727
declare_clippy_lint! {
@@ -583,8 +583,7 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
583583
)
584584
},
585585
ExprKind::Call(path, [arg]) => {
586-
if expr_path_res(cx, path)
587-
.opt_def_id()
586+
if path_def_id(cx, path)
588587
.and_then(|id| match_any_def_paths(cx, id, &[&paths::FROM_STR_METHOD, &paths::FROM_FROM]))
589588
.is_some()
590589
{

clippy_lints/src/ptr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_the
44
use clippy_utils::source::snippet_opt;
55
use clippy_utils::ty::expr_sig;
66
use clippy_utils::{
7-
expr_path_res, get_expr_use_or_unification_node, is_lint_allowed, match_any_diagnostic_items, path_to_local, paths,
7+
expr_path_res, get_expr_use_or_unification_node, is_lint_allowed, is_lint_allowed, match_any_diagnostic_items,
8+
path_def_id, path_to_local, paths, paths,
89
};
910
use if_chain::if_chain;
1011
use rustc_errors::Applicability;
@@ -665,8 +666,8 @@ fn get_rptr_lm<'tcx>(ty: &'tcx hir::Ty<'tcx>) -> Option<(&'tcx Lifetime, Mutabil
665666

666667
fn is_null_path(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
667668
if let ExprKind::Call(pathexp, []) = expr.kind {
668-
expr_path_res(cx, pathexp).opt_def_id().map_or(false, |id| {
669-
match_any_diagnostic_items(cx, id, &[sym::ptr_null, sym::ptr_null_mut]).is_some()
669+
path_def_id(cx, pathexp).map_or(false, |id| {
670+
matches!(cx.tcx.get_diagnostic_name(id), Some(sym::ptr_null | sym::ptr_null_mut))
670671
})
671672
} else {
672673
false

clippy_utils/src/lib.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,6 @@ pub fn match_qpath(path: &QPath<'_>, segments: &[&str]) -> bool {
357357
}
358358
}
359359

360-
/// If the expression is a path, resolve it. Otherwise, return `Res::Err`.
361-
pub fn expr_path_res(cx: &LateContext<'_>, expr: &Expr<'_>) -> Res {
362-
if let ExprKind::Path(p) = &expr.kind {
363-
cx.qpath_res(p, expr.hir_id)
364-
} else {
365-
Res::Err
366-
}
367-
}
368-
369360
/// Resolves the path to a `DefId` and checks if it matches the given path.
370361
pub fn is_qpath_def_path(cx: &LateContext<'_>, path: &QPath<'_>, hir_id: HirId, segments: &[&str]) -> bool {
371362
cx.qpath_res(path, hir_id)
@@ -377,17 +368,13 @@ pub fn is_qpath_def_path(cx: &LateContext<'_>, path: &QPath<'_>, hir_id: HirId,
377368
///
378369
/// Please use `is_expr_diagnostic_item` if the target is a diagnostic item.
379370
pub fn is_expr_path_def_path(cx: &LateContext<'_>, expr: &Expr<'_>, segments: &[&str]) -> bool {
380-
expr_path_res(cx, expr)
381-
.opt_def_id()
382-
.map_or(false, |id| match_def_path(cx, id, segments))
371+
path_def_id(cx, expr).map_or(false, |id| match_def_path(cx, id, segments))
383372
}
384373

385374
/// If the expression is a path, resolves it to a `DefId` and checks if it matches the given
386375
/// diagnostic item.
387376
pub fn is_expr_diagnostic_item(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol) -> bool {
388-
expr_path_res(cx, expr)
389-
.opt_def_id()
390-
.map_or(false, |id| cx.tcx.is_diagnostic_item(diag_item, id))
377+
path_def_id(cx, expr).map_or(false, |id| cx.tcx.is_diagnostic_item(diag_item, id))
391378
}
392379

393380
/// THIS METHOD IS DEPRECATED and will eventually be removed since it does not match against the

clippy_utils/src/ty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_trait_selection::infer::InferCtxtExt;
2020
use rustc_trait_selection::traits::query::normalize::AtExt;
2121
use std::iter;
2222

23-
use crate::{expr_path_res, match_def_path, must_use_attr};
23+
use crate::{match_def_path, must_use_attr, path_res};
2424

2525
// Checks if the given type implements copy.
2626
pub fn is_copy<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
@@ -443,7 +443,7 @@ impl<'tcx> ExprFnSig<'tcx> {
443443

444444
/// If the expression is function like, get the signature for it.
445445
pub fn expr_sig<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option<ExprFnSig<'tcx>> {
446-
if let Res::Def(DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::AssocFn, id) = expr_path_res(cx, expr) {
446+
if let Res::Def(DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) | DefKind::AssocFn, id) = path_res(cx, expr) {
447447
Some(ExprFnSig::Sig(cx.tcx.fn_sig(id)))
448448
} else {
449449
let ty = cx.typeck_results().expr_ty_adjusted(expr).peel_refs();

0 commit comments

Comments
 (0)