Skip to content

Commit 368eb88

Browse files
committed
Refactor MaybePath utils.
* Move to `clippy_utils::paths` * Move functions into a trait on `LateContext`
1 parent 5ac9657 commit 368eb88

File tree

80 files changed

+515
-481
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+515
-481
lines changed

clippy_lints/src/assertions_on_result_states.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ use clippy_utils::macros::{PanicExpn, find_assert_args, root_macro_call_first_no
33
use clippy_utils::source::snippet_with_context;
44
use clippy_utils::ty::{has_debug_impl, is_copy, is_type_diagnostic_item};
55
use clippy_utils::usage::local_used_after_expr;
6-
use clippy_utils::{path_res, sym};
6+
use clippy_utils::{path_to_local, sym};
77
use rustc_errors::Applicability;
8-
use rustc_hir::def::Res;
98
use rustc_hir::{Expr, ExprKind, Node};
109
use rustc_lint::{LateContext, LateLintPass};
1110
use rustc_middle::ty::{self, Ty};
@@ -61,7 +60,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnResultStates {
6160
if !is_copy(cx, result_type) {
6261
if result_type_with_refs != result_type {
6362
return;
64-
} else if let Res::Local(binding_id) = path_res(cx, recv)
63+
} else if let Some(binding_id) = path_to_local(recv)
6564
&& local_used_after_expr(cx, binding_id, recv)
6665
{
6766
return;

clippy_lints/src/box_default.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_default_equivalent;
23
use clippy_utils::macros::macro_backtrace;
4+
use clippy_utils::paths::PathRes;
35
use clippy_utils::ty::expr_sig;
4-
use clippy_utils::{is_default_equivalent, path_def_id};
56
use rustc_errors::Applicability;
67
use rustc_hir::def::Res;
78
use rustc_hir::intravisit::{InferKind, Visitor, VisitorExt, walk_ty};
8-
use rustc_hir::{AmbigArg, Block, Expr, ExprKind, HirId, LetStmt, Node, QPath, Ty, TyKind};
9+
use rustc_hir::{AmbigArg, Block, Expr, ExprKind, HirId, LangItem, LetStmt, Node, QPath, Ty, TyKind};
910
use rustc_lint::{LateContext, LateLintPass, LintContext};
1011
use rustc_session::declare_lint_pass;
1112
use rustc_span::{Span, sym};
@@ -44,7 +45,7 @@ impl LateLintPass<'_> for BoxDefault {
4445
// And that method is `new`
4546
&& seg.ident.name == sym::new
4647
// And the call is that of a `Box` method
47-
&& path_def_id(cx, ty).is_some_and(|id| Some(id) == cx.tcx.lang_items().owned_box())
48+
&& cx.is_path_lang_item(ty, LangItem::OwnedBox)
4849
// And the single argument to the call is another function call
4950
// This is the `T::default()` (or default equivalent) of `Box::new(T::default())`
5051
&& let ExprKind::Call(arg_path, _) = arg.kind

clippy_lints/src/casts/manual_dangling_ptr.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::SpanRangeExt;
3-
use clippy_utils::{expr_or_init, is_path_diagnostic_item, std_or_core, sym};
3+
use clippy_utils::{expr_or_init, std_or_core, sym};
44
use rustc_ast::LitKind;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, GenericArg, Mutability, QPath, Ty, TyKind};
@@ -52,8 +52,11 @@ fn is_expr_const_aligned(cx: &LateContext<'_>, expr: &Expr<'_>, to: &Ty<'_>) ->
5252
}
5353

5454
fn is_align_of_call(cx: &LateContext<'_>, fun: &Expr<'_>, to: &Ty<'_>) -> bool {
55-
if let ExprKind::Path(QPath::Resolved(_, path)) = fun.kind
56-
&& is_path_diagnostic_item(cx, fun, sym::mem_align_of)
55+
if let ExprKind::Path(QPath::Resolved(None, path)) = fun.kind
56+
&& path
57+
.res
58+
.opt_def_id()
59+
.is_some_and(|did| cx.tcx.is_diagnostic_item(sym::mem_align_of, did))
5760
&& let Some(args) = path.segments.last().and_then(|seg| seg.args)
5861
&& let [GenericArg::Type(generic_ty)] = args.args
5962
{

clippy_lints/src/error_impl_error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_hir_and_then};
2-
use clippy_utils::path_res;
2+
use clippy_utils::paths::PathRes;
33
use clippy_utils::ty::implements_trait;
44
use rustc_hir::def_id::{DefId, LocalDefId};
55
use rustc_hir::{Item, ItemKind};
@@ -55,7 +55,7 @@ impl<'tcx> LateLintPass<'tcx> for ErrorImplError {
5555
if let Some(trait_def_id) = imp.of_trait.and_then(|t| t.trait_ref.trait_def_id())
5656
&& let Some(error_def_id) = cx.tcx.get_diagnostic_item(sym::Error)
5757
&& error_def_id == trait_def_id
58-
&& let Some(def_id) = path_res(cx, imp.self_ty).opt_def_id().and_then(DefId::as_local)
58+
&& let Some(def_id) = cx.path_def_id(imp.self_ty).and_then(DefId::as_local)
5959
&& let Some(ident) = cx.tcx.opt_item_ident(def_id.to_def_id())
6060
&& ident.name == sym::Error
6161
&& is_visible_outside_module(cx, def_id) =>

clippy_lints/src/explicit_write.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::{FormatArgsStorage, format_args_inputs_span};
3+
use clippy_utils::paths::PathRes;
34
use clippy_utils::source::snippet_with_applicability;
4-
use clippy_utils::{is_expn_of, path_def_id, sym};
5+
use clippy_utils::{is_expn_of, sym};
56
use rustc_errors::Applicability;
67
use rustc_hir::def::Res;
78
use rustc_hir::{BindingMode, Block, BlockCheckMode, Expr, ExprKind, Node, PatKind, QPath, Stmt, StmtKind};
@@ -59,12 +60,12 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
5960
&& let ExprKind::MethodCall(write_fun, write_recv, [write_arg], _) = *look_in_block(cx, &write_call.kind)
6061
&& let ExprKind::Call(write_recv_path, []) = write_recv.kind
6162
&& write_fun.ident.name == sym::write_fmt
62-
&& let Some(def_id) = path_def_id(cx, write_recv_path)
63+
&& let Some(diag_name) = cx.path_diag_name(write_recv_path)
6364
{
6465
// match calls to std::io::stdout() / std::io::stderr ()
65-
let (dest_name, prefix) = match cx.tcx.get_diagnostic_name(def_id) {
66-
Some(sym::io_stdout) => ("stdout", ""),
67-
Some(sym::io_stderr) => ("stderr", "e"),
66+
let (dest_name, prefix) = match diag_name {
67+
sym::io_stdout => ("stdout", ""),
68+
sym::io_stderr => ("stderr", "e"),
6869
_ => return,
6970
};
7071
let Some(format_args) = self.format_args.get(cx, write_arg, ExpnId::root()) else {

clippy_lints/src/from_over_into.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_config::Conf;
44
use clippy_utils::diagnostics::span_lint_and_then;
55
use clippy_utils::macros::span_is_local;
66
use clippy_utils::msrvs::{self, Msrv};
7-
use clippy_utils::path_def_id;
7+
use clippy_utils::paths::PathRes;
88
use clippy_utils::source::SpanRangeExt;
99
use rustc_errors::Applicability;
1010
use rustc_hir::intravisit::{Visitor, walk_path};
@@ -90,7 +90,7 @@ impl<'tcx> LateLintPass<'tcx> for FromOverInto {
9090
|diag| {
9191
// If the target type is likely foreign mention the orphan rules as it's a common source of
9292
// confusion
93-
if path_def_id(cx, target_ty.peel_refs()).is_none_or(|id| !id.is_local()) {
93+
if cx.path_def_id(target_ty.peel_refs()).is_none_or(|id| !id.is_local()) {
9494
diag.help(
9595
"`impl From<Local> for Foreign` is allowed by the orphan rules, for more information see\n\
9696
https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence"

clippy_lints/src/from_raw_with_void_ptr.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::paths::PathRes;
3+
use clippy_utils::sym;
24
use clippy_utils::ty::is_c_void;
3-
use clippy_utils::{path_def_id, sym};
45
use rustc_hir::def_id::DefId;
56
use rustc_hir::{Expr, ExprKind, QPath};
67
use rustc_lint::{LateContext, LateLintPass};
@@ -41,7 +42,7 @@ impl LateLintPass<'_> for FromRawWithVoidPtr {
4142
if let ExprKind::Call(box_from_raw, [arg]) = expr.kind
4243
&& let ExprKind::Path(QPath::TypeRelative(ty, seg)) = box_from_raw.kind
4344
&& seg.ident.name == sym::from_raw
44-
&& let Some(type_str) = path_def_id(cx, ty).and_then(|id| def_id_matches_type(cx, id))
45+
&& let Some(type_str) = cx.path_def_id(ty).and_then(|id| def_id_matches_type(cx, id))
4546
&& let arg_kind = cx.typeck_results().expr_ty(arg).kind()
4647
&& let ty::RawPtr(ty, _) = arg_kind
4748
&& is_c_void(cx, *ty)
@@ -62,22 +63,14 @@ impl LateLintPass<'_> for FromRawWithVoidPtr {
6263
/// Checks whether a `DefId` matches `Box`, `Rc`, `Arc`, or one of the `Weak` types.
6364
/// Returns a static string slice with the name of the type, if one was found.
6465
fn def_id_matches_type(cx: &LateContext<'_>, def_id: DefId) -> Option<&'static str> {
65-
// Box
6666
if Some(def_id) == cx.tcx.lang_items().owned_box() {
6767
return Some("Box");
6868
}
6969

70-
if let Some(symbol) = cx.tcx.get_diagnostic_name(def_id) {
71-
if symbol == sym::Arc {
72-
return Some("Arc");
73-
} else if symbol == sym::Rc {
74-
return Some("Rc");
75-
}
76-
}
77-
78-
if matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::RcWeak | sym::ArcWeak)) {
79-
Some("Weak")
80-
} else {
81-
None
70+
match cx.tcx.get_diagnostic_name(def_id) {
71+
Some(sym::Arc) => Some("Arc"),
72+
Some(sym::Rc) => Some("Rc"),
73+
Some(sym::RcWeak | sym::ArcWeak) => Some("Weak"),
74+
_ => None,
8275
}
8376
}

clippy_lints/src/if_then_some_else_none.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
44
use clippy_utils::msrvs::{self, Msrv};
5+
use clippy_utils::paths::PathRes;
56
use clippy_utils::source::{snippet_with_applicability, snippet_with_context, walk_span_to_context};
67
use clippy_utils::sugg::Sugg;
78
use clippy_utils::{
8-
contains_return, expr_adjustment_requires_coercion, higher, is_else_clause, is_in_const_context, is_res_lang_ctor,
9-
path_res, peel_blocks, sym,
9+
contains_return, expr_adjustment_requires_coercion, higher, is_else_clause, is_in_const_context, peel_blocks, sym,
1010
};
1111
use rustc_errors::Applicability;
1212
use rustc_hir::LangItem::{OptionNone, OptionSome};
@@ -73,8 +73,8 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
7373
&& let ExprKind::Call(then_call, [then_arg]) = then_expr.kind
7474
&& !expr.span.from_expansion()
7575
&& !then_expr.span.from_expansion()
76-
&& is_res_lang_ctor(cx, path_res(cx, then_call), OptionSome)
77-
&& is_res_lang_ctor(cx, path_res(cx, peel_blocks(els)), OptionNone)
76+
&& cx.is_path_lang_ctor(then_call, OptionSome)
77+
&& cx.is_path_lang_ctor(peel_blocks(els), OptionNone)
7878
&& !is_else_clause(cx.tcx, expr)
7979
&& !is_in_const_context(cx)
8080
&& self.msrv.meets(cx, msrvs::BOOL_THEN)

clippy_lints/src/instant_subtraction.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::msrvs::{self, Msrv};
4+
use clippy_utils::paths::PathRes;
45
use clippy_utils::source::snippet_with_context;
56
use clippy_utils::sugg::Sugg;
6-
use clippy_utils::{is_path_diagnostic_item, ty};
7+
use clippy_utils::ty;
78
use rustc_errors::Applicability;
89
use rustc_hir::{BinOpKind, Expr, ExprKind};
910
use rustc_lint::{LateContext, LateLintPass};
@@ -107,7 +108,7 @@ impl LateLintPass<'_> for InstantSubtraction {
107108

108109
fn is_instant_now_call(cx: &LateContext<'_>, expr_block: &'_ Expr<'_>) -> bool {
109110
if let ExprKind::Call(fn_expr, []) = expr_block.kind
110-
&& is_path_diagnostic_item(cx, fn_expr, sym::instant_now)
111+
&& cx.is_path_diag_item(fn_expr, sym::instant_now)
111112
{
112113
true
113114
} else {

clippy_lints/src/loops/manual_find.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use super::MANUAL_FIND;
22
use super::utils::make_iterator_snippet;
33
use clippy_utils::diagnostics::span_lint_and_then;
4+
use clippy_utils::paths::PathRes;
45
use clippy_utils::source::snippet_with_applicability;
56
use clippy_utils::ty::implements_trait;
67
use clippy_utils::usage::contains_return_break_continue_macro;
7-
use clippy_utils::{higher, is_res_lang_ctor, path_res, peel_blocks_with_stmt};
8+
use clippy_utils::{higher, path_to_local_id, peel_blocks_with_stmt};
89
use rustc_errors::Applicability;
9-
use rustc_hir::def::Res;
1010
use rustc_hir::lang_items::LangItem;
1111
use rustc_hir::{BindingMode, Block, Expr, ExprKind, HirId, Node, Pat, PatKind, Stmt, StmtKind};
1212
use rustc_lint::LateContext;
@@ -34,8 +34,8 @@ pub(super) fn check<'tcx>(
3434
&& let StmtKind::Semi(semi) = stmt.kind
3535
&& let ExprKind::Ret(Some(ret_value)) = semi.kind
3636
&& let ExprKind::Call(ctor, [inner_ret]) = ret_value.kind
37-
&& is_res_lang_ctor(cx, path_res(cx, ctor), LangItem::OptionSome)
38-
&& path_res(cx, inner_ret) == Res::Local(binding_id)
37+
&& cx.is_path_lang_ctor(ctor, LangItem::OptionSome)
38+
&& path_to_local_id(inner_ret, binding_id)
3939
&& !contains_return_break_continue_macro(cond)
4040
&& let Some((last_stmt, last_ret)) = last_stmt_and_ret(cx, expr)
4141
{
@@ -150,7 +150,7 @@ fn last_stmt_and_ret<'tcx>(
150150
&& let Some((_, Node::Block(block))) = parent_iter.next()
151151
&& let Some((last_stmt, last_ret)) = extract(block)
152152
&& last_stmt.hir_id == node_hir
153-
&& is_res_lang_ctor(cx, path_res(cx, last_ret), LangItem::OptionNone)
153+
&& cx.is_path_lang_ctor(last_ret, LangItem::OptionNone)
154154
&& let Some((_, Node::Expr(_block))) = parent_iter.next()
155155
// This includes the function header
156156
&& let Some((_, func)) = parent_iter.next()

0 commit comments

Comments
 (0)