Skip to content

Commit 76bd5d2

Browse files
committed
Refactor diagnostic item methods
1 parent e9728b8 commit 76bd5d2

File tree

6 files changed

+53
-45
lines changed

6 files changed

+53
-45
lines changed

clippy_lints/src/mem_replace.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::source::{snippet, snippet_with_applicability};
3-
use clippy_utils::{in_macro, match_def_path, meets_msrv, paths};
4-
use clippy_utils::{is_diagnostic_assoc_item, is_lang_ctor};
3+
use clippy_utils::{in_macro, is_diag_trait_item, is_lang_ctor, match_def_path, meets_msrv, paths};
54
use if_chain::if_chain;
65
use rustc_errors::Applicability;
76
use rustc_hir::def_id::DefId;
@@ -211,17 +210,17 @@ fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath<
211210
sym::BinaryHeap,
212211
];
213212

214-
if std_types_symbols
215-
.iter()
216-
.any(|symbol| is_diagnostic_assoc_item(cx, def_id, *symbol))
217-
{
218-
if let QPath::TypeRelative(_, method) = path {
219-
if method.ident.name == sym::new {
220-
return true;
213+
if let QPath::TypeRelative(_, method) = path {
214+
if method.ident.name == sym::new {
215+
if let Some(impl_did) = cx.tcx.impl_of_method(def_id) {
216+
if let Some(adt) = cx.tcx.type_of(impl_did).ty_adt_def() {
217+
return std_types_symbols
218+
.iter()
219+
.any(|&symbol| cx.tcx.is_diagnostic_item(symbol, adt.did));
220+
}
221221
}
222222
}
223223
}
224-
225224
false
226225
}
227226

@@ -231,7 +230,7 @@ fn check_replace_with_default(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<
231230
if !in_external_macro(cx.tcx.sess, expr_span);
232231
if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
233232
if let Some(repl_def_id) = cx.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
234-
if is_diagnostic_assoc_item(cx, repl_def_id, sym::Default)
233+
if is_diag_trait_item(cx, repl_def_id, sym::Default)
235234
|| is_default_equivalent_ctor(cx, repl_def_id, repl_func_qpath);
236235

237236
then {

clippy_lints/src/methods/implicit_clone.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::{is_diag_item_method, is_diag_trait_item};
23
use if_chain::if_chain;
34
use rustc_errors::Applicability;
45
use rustc_hir as hir;
5-
use rustc_hir::ExprKind;
66
use rustc_lint::LateContext;
77
use rustc_middle::ty::TyS;
8-
use rustc_span::symbol::Symbol;
8+
use rustc_span::{sym, Span};
99

1010
use super::IMPLICIT_CLONE;
11-
use clippy_utils::is_diagnostic_assoc_item;
1211

13-
pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, trait_diagnostic: Symbol) {
12+
pub fn check(cx: &LateContext<'_>, method_name: &str, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, span: Span) {
1413
if_chain! {
15-
if let ExprKind::MethodCall(method_path, _, [arg], _) = &expr.kind;
14+
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
15+
if match method_name {
16+
"to_os_string" => is_diag_item_method(cx, method_def_id, sym::OsStr),
17+
"to_owned" => is_diag_trait_item(cx, method_def_id, sym::ToOwned),
18+
"to_path_buf" => is_diag_item_method(cx, method_def_id, sym::Path),
19+
"to_vec" => cx.tcx.impl_of_method(method_def_id)
20+
.map(|impl_did| Some(impl_did) == cx.tcx.lang_items().slice_alloc_impl())
21+
== Some(true),
22+
_ => false,
23+
};
1624
let return_type = cx.typeck_results().expr_ty(expr);
17-
let input_type = cx.typeck_results().expr_ty(arg).peel_refs();
18-
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
25+
let input_type = cx.typeck_results().expr_ty(recv).peel_refs();
1926
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did));
2027
if TyS::same_type(return_type, input_type);
21-
if is_diagnostic_assoc_item(cx, expr_def_id, trait_diagnostic);
2228
then {
2329
span_lint_and_sugg(
24-
cx,IMPLICIT_CLONE,method_path.ident.span,
25-
&format!("implicitly cloning a `{}` by calling `{}` on its dereferenced type", ty_name, method_path.ident.name),
30+
cx,
31+
IMPLICIT_CLONE,
32+
span,
33+
&format!("implicitly cloning a `{}` by calling `{}` on its dereferenced type", ty_name, method_name),
2634
"consider using",
2735
"clone".to_string(),
2836
Applicability::MachineApplicable

clippy_lints/src/methods/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1987,10 +1987,9 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
19871987
}
19881988
},
19891989
("step_by", [arg]) => iterator_step_by_zero::check(cx, expr, arg),
1990-
("to_os_string", []) => implicit_clone::check(cx, expr, sym::OsStr),
1991-
("to_owned", []) => implicit_clone::check(cx, expr, sym::ToOwned),
1992-
("to_path_buf", []) => implicit_clone::check(cx, expr, sym::Path),
1993-
("to_vec", []) => implicit_clone::check(cx, expr, sym::slice),
1990+
("to_os_string" | "to_owned" | "to_path_buf" | "to_vec", []) => {
1991+
implicit_clone::check(cx, name, expr, recv, span);
1992+
},
19941993
("unwrap", []) => match method_call!(recv) {
19951994
Some(("get", [recv, get_arg], _)) => get_unwrap::check(cx, expr, recv, get_arg, false),
19961995
Some(("get_mut", [recv, get_arg], _)) => get_unwrap::check(cx, expr, recv, get_arg, true),

clippy_lints/src/misc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_span::symbol::sym;
2020
use crate::consts::{constant, Constant};
2121
use clippy_utils::sugg::Sugg;
2222
use clippy_utils::{
23-
get_item_name, get_parent_expr, higher, in_constant, is_diagnostic_assoc_item, is_integer_const, iter_input_pats,
23+
get_item_name, get_parent_expr, higher, in_constant, is_diag_trait_item, is_integer_const, iter_input_pats,
2424
last_path_segment, match_qpath, unsext, SpanlessEq,
2525
};
2626

@@ -555,8 +555,8 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
555555
ExprKind::MethodCall(.., args, _) if args.len() == 1 => {
556556
if_chain!(
557557
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
558-
if is_diagnostic_assoc_item(cx, expr_def_id, sym::ToString)
559-
|| is_diagnostic_assoc_item(cx, expr_def_id, sym::ToOwned);
558+
if is_diag_trait_item(cx, expr_def_id, sym::ToString)
559+
|| is_diag_trait_item(cx, expr_def_id, sym::ToOwned);
560560
then {
561561
(cx.typeck_results().expr_ty(&args[0]), snippet(cx, args[0].span, ".."))
562562
} else {

clippy_lints/src/to_string_in_display.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::{is_diagnostic_assoc_item, match_def_path, path_to_local_id, paths};
2+
use clippy_utils::{is_diag_trait_item, match_def_path, path_to_local_id, paths};
33
use if_chain::if_chain;
44
use rustc_hir::{Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass};
@@ -95,7 +95,7 @@ impl LateLintPass<'_> for ToStringInDisplay {
9595
if let ExprKind::MethodCall(path, _, args, _) = expr.kind;
9696
if path.ident.name == sym!(to_string);
9797
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
98-
if is_diagnostic_assoc_item(cx, expr_def_id, sym::ToString);
98+
if is_diag_trait_item(cx, expr_def_id, sym::ToString);
9999
if path_to_local_id(&args[0], self_hir_id);
100100
then {
101101
span_lint(

clippy_utils/src/lib.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -293,27 +293,29 @@ pub fn match_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, path: &[&str])
293293
trt_id.map_or(false, |trt_id| match_def_path(cx, trt_id, path))
294294
}
295295

296-
/// Checks if the method call given in `def_id` belongs to a trait or other container with a given
297-
/// diagnostic item
298-
pub fn is_diagnostic_assoc_item(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
299-
cx.tcx
300-
.opt_associated_item(def_id)
301-
.and_then(|associated_item| match associated_item.container {
302-
rustc_ty::TraitContainer(assoc_def_id) => Some(assoc_def_id),
303-
rustc_ty::ImplContainer(assoc_def_id) => match cx.tcx.type_of(assoc_def_id).kind() {
304-
rustc_ty::Adt(adt, _) => Some(adt.did),
305-
rustc_ty::Slice(_) => cx.tcx.get_diagnostic_item(sym::slice), // this isn't perfect but it works
306-
_ => None,
307-
},
308-
})
309-
.map_or(false, |assoc_def_id| cx.tcx.is_diagnostic_item(diag_item, assoc_def_id))
296+
/// Checks if a method is defined in an impl of a diagnostic item
297+
pub fn is_diag_item_method(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
298+
if let Some(impl_did) = cx.tcx.impl_of_method(def_id) {
299+
if let Some(adt) = cx.tcx.type_of(impl_did).ty_adt_def() {
300+
return cx.tcx.is_diagnostic_item(diag_item, adt.did);
301+
}
302+
}
303+
false
304+
}
305+
306+
/// Checks if a method is in a diagnostic item trait
307+
pub fn is_diag_trait_item(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
308+
if let Some(trait_did) = cx.tcx.trait_of_item(def_id) {
309+
return cx.tcx.is_diagnostic_item(diag_item, trait_did);
310+
}
311+
false
310312
}
311313

312314
/// Checks if the method call given in `expr` belongs to the given trait.
313315
pub fn is_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol) -> bool {
314316
cx.typeck_results()
315317
.type_dependent_def_id(expr.hir_id)
316-
.map_or(false, |did| is_diagnostic_assoc_item(cx, did, diag_item))
318+
.map_or(false, |did| is_diag_trait_item(cx, did, diag_item))
317319
}
318320

319321
/// Checks if an expression references a variable of the given name.

0 commit comments

Comments
 (0)