Skip to content

Commit 2e6729e

Browse files
committed
Remove is_diag_trait_item
1 parent a6078f8 commit 2e6729e

File tree

12 files changed

+102
-102
lines changed

12 files changed

+102
-102
lines changed

clippy_lints/src/assigning_clones.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::mir::{PossibleBorrowerMap, enclosing_mir};
44
use clippy_utils::msrvs::{self, Msrv};
5-
use clippy_utils::res::MaybeResPath;
5+
use clippy_utils::res::{MaybeDef, MaybeResPath};
66
use clippy_utils::sugg::Sugg;
7-
use clippy_utils::{is_diag_trait_item, is_in_test, last_path_segment, local_is_initialized, sym};
7+
use clippy_utils::{is_in_test, last_path_segment, local_is_initialized, sym};
88
use rustc_errors::Applicability;
99
use rustc_hir::{self as hir, Expr, ExprKind};
1010
use rustc_lint::{LateContext, LateLintPass};
@@ -69,32 +69,32 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
6969
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
7070
if let ExprKind::Assign(lhs, rhs, _) = e.kind
7171
&& let typeck = cx.typeck_results()
72-
&& let (call_kind, fn_name, fn_id, fn_arg, fn_gen_args) = match rhs.kind {
72+
&& let (call_kind, fn_name, fn_def, fn_arg, fn_gen_args) = match rhs.kind {
7373
ExprKind::Call(f, [arg])
7474
if let ExprKind::Path(fn_path) = &f.kind
75-
&& let Some(id) = typeck.qpath_res(fn_path, f.hir_id).opt_def_id() =>
75+
&& let Some(def) = typeck.qpath_res(fn_path, f.hir_id).opt_def(cx) =>
7676
{
77-
(CallKind::Ufcs, last_path_segment(fn_path).ident.name, id, arg, typeck.node_args(f.hir_id))
77+
(CallKind::Ufcs, last_path_segment(fn_path).ident.name, def, arg, typeck.node_args(f.hir_id))
7878
},
79-
ExprKind::MethodCall(name, recv, [], _) if let Some(id) = typeck.type_dependent_def_id(rhs.hir_id) => {
80-
(CallKind::Method, name.ident.name, id, recv, typeck.node_args(rhs.hir_id))
79+
ExprKind::MethodCall(name, recv, [], _) if let Some(def) = typeck.type_dependent_def(rhs.hir_id) => {
80+
(CallKind::Method, name.ident.name, def, recv, typeck.node_args(rhs.hir_id))
8181
},
8282
_ => return,
8383
}
8484
&& let ctxt = e.span.ctxt()
8585
// Don't lint in macros.
8686
&& ctxt.is_root()
8787
&& let which_trait = match fn_name {
88-
sym::clone if is_diag_trait_item(cx, fn_id, sym::Clone) => CloneTrait::Clone,
88+
sym::clone if fn_def.assoc_fn_parent(cx).is_diag_item(cx, sym::Clone) => CloneTrait::Clone,
8989
sym::to_owned
90-
if is_diag_trait_item(cx, fn_id, sym::ToOwned)
90+
if fn_def.assoc_fn_parent(cx).is_diag_item(cx, sym::ToOwned)
9191
&& self.msrv.meets(cx, msrvs::CLONE_INTO) =>
9292
{
9393
CloneTrait::ToOwned
9494
},
9595
_ => return,
9696
}
97-
&& let Ok(Some(resolved_fn)) = Instance::try_resolve(cx.tcx, cx.typing_env(), fn_id, fn_gen_args)
97+
&& let Ok(Some(resolved_fn)) = Instance::try_resolve(cx.tcx, cx.typing_env(), fn_def.1, fn_gen_args)
9898
// TODO: This check currently bails if the local variable has no initializer.
9999
// That is overly conservative - the lint should fire even if there was no initializer,
100100
// but the variable has been initialized before `lhs` was evaluated.

clippy_lints/src/format_args.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use clippy_utils::msrvs::{self, Msrv};
1212
use clippy_utils::res::MaybeDef;
1313
use clippy_utils::source::{SpanRangeExt, snippet};
1414
use clippy_utils::ty::implements_trait;
15-
use clippy_utils::{is_diag_trait_item, is_from_proc_macro, is_in_test, trait_ref_of_method};
15+
use clippy_utils::{is_from_proc_macro, is_in_test, trait_ref_of_method};
1616
use itertools::Itertools;
1717
use rustc_ast::{
1818
FormatArgPosition, FormatArgPositionKind, FormatArgsPiece, FormatArgumentKind, FormatCount, FormatOptions,
@@ -498,8 +498,11 @@ impl<'tcx> FormatArgsExpr<'_, 'tcx> {
498498
let cx = self.cx;
499499
if !value.span.from_expansion()
500500
&& let ExprKind::MethodCall(_, receiver, [], to_string_span) = value.kind
501-
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(value.hir_id)
502-
&& is_diag_trait_item(cx, method_def_id, sym::ToString)
501+
&& cx
502+
.typeck_results()
503+
.type_dependent_def_id(value.hir_id)
504+
.opt_parent(cx)
505+
.is_diag_item(cx, sym::ToString)
503506
&& let receiver_ty = cx.typeck_results().expr_ty(receiver)
504507
&& let Some(display_trait_id) = cx.tcx.get_diagnostic_item(sym::Display)
505508
&& let (n_needed_derefs, target) =

clippy_lints/src/format_impl.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
22
use clippy_utils::macros::{FormatArgsStorage, find_format_arg_expr, is_format_macro, root_macro_call_first_node};
3-
use clippy_utils::res::MaybeResPath;
4-
use clippy_utils::{get_parent_as_impl, is_diag_trait_item, peel_ref_operators, sym};
3+
use clippy_utils::res::{MaybeDef, MaybeResPath};
4+
use clippy_utils::{get_parent_as_impl, peel_ref_operators, sym};
55
use rustc_ast::{FormatArgsPiece, FormatTrait};
66
use rustc_errors::Applicability;
77
use rustc_hir::{Expr, ExprKind, Impl, ImplItem, ImplItemKind, QPath};
@@ -158,8 +158,12 @@ impl FormatImplExpr<'_, '_> {
158158
&& path.ident.name == sym::to_string
159159
// Is the method a part of the ToString trait? (i.e. not to_string() implemented
160160
// separately)
161-
&& let Some(expr_def_id) = self.cx.typeck_results().type_dependent_def_id(self.expr.hir_id)
162-
&& is_diag_trait_item(self.cx, expr_def_id, sym::ToString)
161+
&& self
162+
.cx
163+
.typeck_results()
164+
.type_dependent_def_id(self.expr.hir_id)
165+
.opt_parent(self.cx)
166+
.is_diag_item(self.cx, sym::ToString)
163167
// Is the method is called on self
164168
&& let ExprKind::Path(QPath::Resolved(_, path)) = self_arg.kind
165169
&& let [segment] = path.segments

clippy_lints/src/manual_clamp.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ use clippy_utils::consts::{ConstEvalCtxt, Constant};
33
use clippy_utils::diagnostics::{span_lint_and_then, span_lint_hir_and_then};
44
use clippy_utils::higher::If;
55
use clippy_utils::msrvs::{self, Msrv};
6-
use clippy_utils::res::MaybeResPath;
6+
use clippy_utils::res::{MaybeDef, MaybeResPath};
77
use clippy_utils::sugg::Sugg;
88
use clippy_utils::ty::implements_trait;
99
use clippy_utils::visitors::is_const_evaluatable;
10-
use clippy_utils::{
11-
eq_expr_value, is_diag_trait_item, is_in_const_context, is_trait_method, peel_blocks, peel_blocks_with_stmt, sym,
12-
};
10+
use clippy_utils::{eq_expr_value, is_in_const_context, is_trait_method, peel_blocks, peel_blocks_with_stmt, sym};
1311
use itertools::Itertools;
1412
use rustc_errors::{Applicability, Diag};
1513
use rustc_hir::def::Res;
@@ -331,11 +329,11 @@ fn is_call_max_min_pattern<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>)
331329
fn segment<'tcx>(cx: &LateContext<'_>, func: &Expr<'tcx>) -> Option<FunctionType<'tcx>> {
332330
match func.kind {
333331
ExprKind::Path(QPath::Resolved(None, path)) => {
334-
let id = path.res.opt_def_id()?;
335-
match cx.tcx.get_diagnostic_name(id) {
332+
let def = path.res.opt_def(cx)?;
333+
match cx.tcx.get_diagnostic_name(def.1) {
336334
Some(sym::cmp_min) => Some(FunctionType::CmpMin),
337335
Some(sym::cmp_max) => Some(FunctionType::CmpMax),
338-
_ if is_diag_trait_item(cx, id, sym::Ord) => {
336+
_ if def.assoc_fn_parent(cx).is_diag_item(cx, sym::Ord) => {
339337
Some(FunctionType::OrdOrFloat(path.segments.last().expect("infallible")))
340338
},
341339
_ => None,

clippy_lints/src/methods/implicit_clone.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::res::MaybeDef;
23
use clippy_utils::source::snippet_with_context;
4+
use clippy_utils::sym;
35
use clippy_utils::ty::{implements_trait, peel_and_count_ty_refs};
4-
use clippy_utils::{is_diag_item_method, is_diag_trait_item, sym};
56
use rustc_errors::Applicability;
67
use rustc_hir as hir;
78
use rustc_lint::LateContext;
@@ -10,12 +11,12 @@ use rustc_span::Symbol;
1011
use super::IMPLICIT_CLONE;
1112

1213
pub fn check(cx: &LateContext<'_>, method_name: Symbol, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
13-
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
14-
&& is_clone_like(cx, method_name, method_def_id)
14+
if let Some(method_parent_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id).opt_parent(cx)
15+
&& is_clone_like(cx, method_name, method_parent_id)
1516
&& let return_type = cx.typeck_results().expr_ty(expr)
1617
&& let input_type = cx.typeck_results().expr_ty(recv)
1718
&& let (input_type, ref_count, _) = peel_and_count_ty_refs(input_type)
18-
&& !(ref_count > 0 && is_diag_trait_item(cx, method_def_id, sym::ToOwned))
19+
&& !(ref_count > 0 && method_parent_id.is_diag_item(cx, sym::ToOwned))
1920
&& let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did()))
2021
&& return_type == input_type
2122
&& let Some(clone_trait) = cx.tcx.lang_items().clone_trait()
@@ -40,19 +41,15 @@ pub fn check(cx: &LateContext<'_>, method_name: Symbol, expr: &hir::Expr<'_>, re
4041
}
4142

4243
/// Returns true if the named method can be used to clone the receiver.
43-
pub fn is_clone_like(cx: &LateContext<'_>, method_name: Symbol, method_def_id: hir::def_id::DefId) -> bool {
44+
pub fn is_clone_like(cx: &LateContext<'_>, method_name: Symbol, method_parent_id: hir::def_id::DefId) -> bool {
4445
match method_name {
45-
sym::to_os_string => is_diag_item_method(cx, method_def_id, sym::OsStr),
46-
sym::to_owned => is_diag_trait_item(cx, method_def_id, sym::ToOwned),
47-
sym::to_path_buf => is_diag_item_method(cx, method_def_id, sym::Path),
48-
sym::to_string => is_diag_trait_item(cx, method_def_id, sym::ToString),
49-
sym::to_vec => cx
50-
.tcx
51-
.impl_of_assoc(method_def_id)
52-
.filter(|&impl_did| {
53-
cx.tcx.type_of(impl_did).instantiate_identity().is_slice() && cx.tcx.impl_trait_ref(impl_did).is_none()
54-
})
55-
.is_some(),
46+
sym::to_os_string => method_parent_id.opt_impl_ty(cx).is_diag_item(cx, sym::OsStr),
47+
sym::to_owned => method_parent_id.is_diag_item(cx, sym::ToOwned),
48+
sym::to_path_buf => method_parent_id.opt_impl_ty(cx).is_diag_item(cx, sym::Path),
49+
sym::to_string => method_parent_id.is_diag_item(cx, sym::ToString),
50+
sym::to_vec => method_parent_id
51+
.opt_impl_ty(cx)
52+
.is_some_and(|ty| ty.instantiate_identity().is_slice()),
5653
_ => false,
5754
}
5855
}

clippy_lints/src/methods/manual_inspect.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::msrvs::{self, Msrv};
3-
use clippy_utils::res::MaybeResPath;
3+
use clippy_utils::res::{MaybeDef, MaybeResPath};
44
use clippy_utils::source::{IntoSpan, SpanRangeExt};
55
use clippy_utils::ty::get_field_by_name;
66
use clippy_utils::visitors::{for_each_expr, for_each_expr_without_closures};
7-
use clippy_utils::{ExprUseNode, expr_use_ctxt, is_diag_item_method, is_diag_trait_item, sym};
7+
use clippy_utils::{ExprUseNode, expr_use_ctxt, is_diag_item_method, sym};
88
use core::ops::ControlFlow;
99
use rustc_errors::Applicability;
1010
use rustc_hir::{BindingMode, BorrowKind, ByRef, ClosureKind, Expr, ExprKind, Mutability, Node, PatKind};
@@ -19,9 +19,9 @@ pub(crate) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, arg: &Expr<'_>, name:
1919
if let ExprKind::Closure(c) = arg.kind
2020
&& matches!(c.kind, ClosureKind::Closure)
2121
&& let typeck = cx.typeck_results()
22-
&& let Some(fn_id) = typeck.type_dependent_def_id(expr.hir_id)
23-
&& (is_diag_trait_item(cx, fn_id, sym::Iterator)
24-
|| ((is_diag_item_method(cx, fn_id, sym::Option) || is_diag_item_method(cx, fn_id, sym::Result))
22+
&& let Some(fn_def) = typeck.type_dependent_def(expr.hir_id)
23+
&& (fn_def.assoc_fn_parent(cx).is_diag_item(cx, sym::Iterator)
24+
|| ((is_diag_item_method(cx, fn_def.1, sym::Option) || is_diag_item_method(cx, fn_def.1, sym::Result))
2525
&& msrv.meets(cx, msrvs::OPTION_RESULT_INSPECT)))
2626
&& let body = cx.tcx.hir_body(c.body)
2727
&& let [param] = body.params

clippy_lints/src/methods/map_clone.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::msrvs::{self, Msrv};
3+
use clippy_utils::peel_blocks;
34
use clippy_utils::res::MaybeDef;
45
use clippy_utils::source::snippet_with_applicability;
56
use clippy_utils::ty::{is_copy, should_call_clone_as_function};
6-
use clippy_utils::{is_diag_trait_item, peel_blocks};
77
use rustc_errors::Applicability;
88
use rustc_hir::def_id::DefId;
99
use rustc_hir::{self as hir, LangItem};
@@ -19,14 +19,13 @@ use super::MAP_CLONE;
1919
// If this `map` is called on an `Option` or a `Result` and the previous call is `as_ref`, we don't
2020
// run this lint because it would overlap with `useless_asref` which provides a better suggestion
2121
// in this case.
22-
fn should_run_lint(cx: &LateContext<'_>, e: &hir::Expr<'_>, method_id: DefId) -> bool {
23-
if is_diag_trait_item(cx, method_id, sym::Iterator) {
22+
fn should_run_lint(cx: &LateContext<'_>, e: &hir::Expr<'_>, method_parent_id: DefId) -> bool {
23+
if method_parent_id.is_diag_item(cx, sym::Iterator) {
2424
return true;
2525
}
2626
// We check if it's an `Option` or a `Result`.
27-
if let Some(id) = cx.tcx.impl_of_assoc(method_id) {
28-
let identity = cx.tcx.type_of(id).instantiate_identity();
29-
if !identity.is_diag_item(cx, sym::Option) && !identity.is_diag_item(cx, sym::Result) {
27+
if let Some(ty) = method_parent_id.opt_impl_ty(cx) {
28+
if !ty.is_diag_item(cx, sym::Option) && !ty.is_diag_item(cx, sym::Result) {
3029
return false;
3130
}
3231
} else {
@@ -43,8 +42,8 @@ fn should_run_lint(cx: &LateContext<'_>, e: &hir::Expr<'_>, method_id: DefId) ->
4342
}
4443

4544
pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>, msrv: Msrv) {
46-
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
47-
&& should_run_lint(cx, e, method_id)
45+
if let Some(parent_id) = cx.typeck_results().type_dependent_def_id(e.hir_id).opt_parent(cx)
46+
&& should_run_lint(cx, e, parent_id)
4847
{
4948
match arg.kind {
5049
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {

clippy_lints/src/methods/suspicious_to_owned.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::is_diag_trait_item;
32
use clippy_utils::res::MaybeDef;
43
use clippy_utils::source::snippet_with_context;
54
use rustc_errors::Applicability;
@@ -11,8 +10,11 @@ use rustc_span::sym;
1110
use super::SUSPICIOUS_TO_OWNED;
1211

1312
pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) -> bool {
14-
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
15-
&& is_diag_trait_item(cx, method_def_id, sym::ToOwned)
13+
if cx
14+
.typeck_results()
15+
.type_dependent_def_id(expr.hir_id)
16+
.opt_parent(cx)
17+
.is_diag_item(cx, sym::ToOwned)
1618
&& let input_type = cx.typeck_results().expr_ty(expr)
1719
&& input_type.is_diag_item(cx, sym::Cow)
1820
{

0 commit comments

Comments
 (0)