Skip to content

Commit cfd9e6e

Browse files
committed
migrate paths to newly-added diagnostic items
This gets rid of the following paths: * OS_STRING * TO_OWNED * TO_STRING
1 parent 7154b23 commit cfd9e6e

File tree

7 files changed

+50
-25
lines changed

7 files changed

+50
-25
lines changed

clippy_lints/src/misc.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ use rustc_middle::ty::{self, Ty};
1212
use rustc_session::{declare_lint_pass, declare_tool_lint};
1313
use rustc_span::hygiene::DesugaringKind;
1414
use rustc_span::source_map::{ExpnKind, Span};
15+
use rustc_span::symbol::sym;
1516

1617
use crate::consts::{constant, Constant};
1718
use crate::utils::sugg::Sugg;
1819
use crate::utils::{
19-
get_item_name, get_parent_expr, higher, implements_trait, in_constant, is_integer_const, iter_input_pats,
20-
last_path_segment, match_qpath, match_trait_method, paths, snippet, snippet_opt, span_lint, span_lint_and_sugg,
20+
get_item_name, get_parent_expr, higher, implements_trait, in_constant, is_diagnostic_assoc_item, is_integer_const,
21+
iter_input_pats, last_path_segment, match_qpath, snippet, snippet_opt, span_lint, span_lint_and_sugg,
2122
span_lint_and_then, span_lint_hir_and_then, unsext, SpanlessEq,
2223
};
2324

@@ -554,11 +555,16 @@ fn check_to_owned(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left:
554555

555556
let (arg_ty, snip) = match expr.kind {
556557
ExprKind::MethodCall(.., ref args, _) if args.len() == 1 => {
557-
if match_trait_method(cx, expr, &paths::TO_STRING) || match_trait_method(cx, expr, &paths::TO_OWNED) {
558-
(cx.typeck_results().expr_ty(&args[0]), snippet(cx, args[0].span, ".."))
559-
} else {
560-
return;
561-
}
558+
if_chain!(
559+
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
560+
if is_diagnostic_assoc_item(cx, expr_def_id, sym::ToString)
561+
|| is_diagnostic_assoc_item(cx, expr_def_id, sym::ToOwned);
562+
then {
563+
(cx.typeck_results().expr_ty(&args[0]), snippet(cx, args[0].span, ".."))
564+
} else {
565+
return;
566+
}
567+
)
562568
},
563569
ExprKind::Call(ref path, ref v) if v.len() == 1 => {
564570
if let ExprKind::Path(ref path) = path.kind {

clippy_lints/src/path_buf_push_overwrite.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use crate::utils::{match_type, paths, span_lint_and_sugg};
1+
use crate::utils::{is_type_diagnostic_item, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc_ast::ast::LitKind;
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_span::symbol::sym;
89
use std::path::{Component, Path};
910

1011
declare_clippy_lint! {
@@ -46,7 +47,7 @@ impl<'tcx> LateLintPass<'tcx> for PathBufPushOverwrite {
4647
if let ExprKind::MethodCall(ref path, _, ref args, _) = expr.kind;
4748
if path.ident.name == sym!(push);
4849
if args.len() == 2;
49-
if match_type(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), &paths::PATH_BUF);
50+
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), sym::PathBuf);
5051
if let Some(get_index_arg) = args.get(1);
5152
if let ExprKind::Lit(ref lit) = get_index_arg.kind;
5253
if let LitKind::Str(ref path_lit, _) = lit.node;

clippy_lints/src/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ fn check_fn(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_id:
233233
},
234234
);
235235
}
236-
} else if match_type(cx, ty, &paths::PATH_BUF) {
236+
} else if is_type_diagnostic_item(cx, ty, sym::PathBuf) {
237237
if let Some(spans) = get_spans(cx, opt_body_id, idx, &[("clone", ".to_path_buf()"), ("as_path", "")]) {
238238
span_lint_and_then(
239239
cx,

clippy_lints/src/redundant_clone.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
166166
is_call_with_ref_arg(cx, mir, &pred_terminator.kind);
167167
if res == cloned;
168168
if match_def_path(cx, pred_fn_def_id, &paths::DEREF_TRAIT_METHOD);
169-
if match_type(cx, pred_arg_ty, &paths::PATH_BUF)
170-
|| match_type(cx, pred_arg_ty, &paths::OS_STRING);
169+
if is_type_diagnostic_item(cx, pred_arg_ty, sym::PathBuf)
170+
|| is_type_diagnostic_item(cx, pred_arg_ty, sym::OsString);
171171
then {
172172
(pred_arg, res)
173173
} else {

clippy_lints/src/to_string_in_display.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use crate::utils::{match_def_path, match_trait_method, path_to_local_id, paths, span_lint};
1+
use crate::utils::{is_diagnostic_assoc_item, match_def_path, path_to_local_id, paths, span_lint};
22
use if_chain::if_chain;
33
use rustc_hir::{Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind};
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_session::{declare_tool_lint, impl_lint_pass};
6+
use rustc_span::symbol::sym;
67

78
declare_clippy_lint! {
89
/// **What it does:** Checks for uses of `to_string()` in `Display` traits.
@@ -92,7 +93,8 @@ impl LateLintPass<'_> for ToStringInDisplay {
9293
if let Some(self_hir_id) = self.self_hir_id;
9394
if let ExprKind::MethodCall(ref path, _, args, _) = expr.kind;
9495
if path.ident.name == sym!(to_string);
95-
if match_trait_method(cx, expr, &paths::TO_STRING);
96+
if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
97+
if is_diagnostic_assoc_item(cx, expr_def_id, sym::ToString);
9698
if path_to_local_id(&args[0], self_hir_id);
9799
then {
98100
span_lint(

clippy_lints/src/types.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_semver::RustcVersion;
2323
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
2424
use rustc_span::hygiene::{ExpnKind, MacroKind};
2525
use rustc_span::source_map::Span;
26-
use rustc_span::symbol::sym;
26+
use rustc_span::symbol::{sym, Symbol};
2727
use rustc_target::abi::LayoutOf;
2828
use rustc_target::spec::abi::Abi;
2929
use rustc_typeck::hir_ty_to_ty;
@@ -307,14 +307,33 @@ fn match_type_parameter(cx: &LateContext<'_>, qpath: &QPath<'_>, path: &[&str])
307307
None
308308
}
309309

310+
/// Checks if `qpath` has last segment with type parameter matching `diagnostic`
311+
fn match_type_parameter_diagnostic(cx: &LateContext<'_>, qpath: &QPath<'_>, diagnostic: Symbol) -> Option<Span> {
312+
let last = last_path_segment(qpath);
313+
if_chain! {
314+
if let Some(ref params) = last.args;
315+
if !params.parenthesized;
316+
if let Some(ty) = params.args.iter().find_map(|arg| match arg {
317+
GenericArg::Type(ty) => Some(ty),
318+
_ => None,
319+
});
320+
if let TyKind::Path(ref qpath) = ty.kind;
321+
if cx.qpath_res(qpath, ty.hir_id).opt_def_id() == cx.tcx.get_diagnostic_item(diagnostic);
322+
then {
323+
return Some(ty.span);
324+
}
325+
}
326+
None
327+
}
328+
310329
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
311-
if match_type_parameter(cx, qpath, &paths::STRING).is_some() {
330+
if match_type_parameter_diagnostic(cx, qpath, sym::string_type).is_some() {
312331
return Some("str");
313332
}
314-
if match_type_parameter(cx, qpath, &paths::OS_STRING).is_some() {
333+
if match_type_parameter_diagnostic(cx, qpath, sym::OsString).is_some() {
315334
return Some("std::ffi::OsStr");
316335
}
317-
if match_type_parameter(cx, qpath, &paths::PATH_BUF).is_some() {
336+
if match_type_parameter_diagnostic(cx, qpath, sym::PathBuf).is_some() {
318337
return Some("std::path::Path");
319338
}
320339
None
@@ -381,7 +400,7 @@ impl Types {
381400
);
382401
return; // don't recurse into the type
383402
}
384-
if match_type_parameter(cx, qpath, &paths::VEC).is_some() {
403+
if match_type_parameter_diagnostic(cx, qpath, sym::vec_type).is_some() {
385404
span_lint_and_help(
386405
cx,
387406
BOX_VEC,
@@ -393,7 +412,7 @@ impl Types {
393412
return; // don't recurse into the type
394413
}
395414
} else if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
396-
if let Some(span) = match_type_parameter(cx, qpath, &paths::RC) {
415+
if let Some(span) = match_type_parameter_diagnostic(cx, qpath, sym::Rc) {
397416
let mut applicability = Applicability::MachineApplicable;
398417
span_lint_and_sugg(
399418
cx,
@@ -445,7 +464,7 @@ impl Types {
445464
);
446465
return; // don't recurse into the type
447466
}
448-
if match_type_parameter(cx, qpath, &paths::VEC).is_some() {
467+
if match_type_parameter_diagnostic(cx, qpath, sym::vec_type).is_some() {
449468
let vec_ty = match &last_path_segment(qpath).args.unwrap().args[0] {
450469
GenericArg::Type(ty) => match &ty.kind {
451470
TyKind::Path(qpath) => qpath,
@@ -498,7 +517,7 @@ impl Types {
498517
);
499518
return; // don't recurse into the type
500519
}
501-
if match_type_parameter(cx, qpath, &paths::VEC).is_some() {
520+
if match_type_parameter_diagnostic(cx, qpath, sym::vec_type).is_some() {
502521
let vec_ty = match &last_path_segment(qpath).args.unwrap().args[0] {
503522
GenericArg::Type(ty) => match &ty.kind {
504523
TyKind::Path(qpath) => qpath,

clippy_utils/src/paths.rs

-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ pub const OPTION: [&str; 3] = ["core", "option", "Option"];
8989
pub const OPTION_NONE: [&str; 4] = ["core", "option", "Option", "None"];
9090
pub const OPTION_SOME: [&str; 4] = ["core", "option", "Option", "Some"];
9191
pub const ORD: [&str; 3] = ["core", "cmp", "Ord"];
92-
pub const OS_STRING: [&str; 4] = ["std", "ffi", "os_str", "OsString"];
9392
pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"];
9493
pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"];
9594
pub(super) const PANICKING_PANIC: [&str; 3] = ["core", "panicking", "panic"];
@@ -162,9 +161,7 @@ pub const SYMBOL_TO_IDENT_STRING: [&str; 4] = ["rustc_span", "symbol", "Symbol",
162161
pub const SYM_MODULE: [&str; 3] = ["rustc_span", "symbol", "sym"];
163162
#[cfg(feature = "internal-lints")]
164163
pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"];
165-
pub const TO_OWNED: [&str; 3] = ["alloc", "borrow", "ToOwned"];
166164
pub const TO_OWNED_METHOD: [&str; 4] = ["alloc", "borrow", "ToOwned", "to_owned"];
167-
pub const TO_STRING: [&str; 3] = ["alloc", "string", "ToString"];
168165
pub const TO_STRING_METHOD: [&str; 4] = ["alloc", "string", "ToString", "to_string"];
169166
pub const TRANSMUTE: [&str; 4] = ["core", "intrinsics", "", "transmute"];
170167
pub const TRY_FROM: [&str; 4] = ["core", "convert", "TryFrom", "try_from"];

0 commit comments

Comments
 (0)