Skip to content

Commit 3877a41

Browse files
committed
migrate paths to newly-added diagnostic items
This gets rid of the following paths: * OS_STRING * TO_OWNED * TO_STRING Also removes some usages of: * PATH_BUF And the now completely unused `clippy_lints::types::is_ty_param_path`
1 parent e451d6e commit 3877a41

File tree

7 files changed

+27
-34
lines changed

7 files changed

+27
-34
lines changed

clippy_lints/src/misc.rs

Lines changed: 13 additions & 7 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{
2-
fn_has_unsatisfiable_preds, has_drop, is_copy, is_type_diagnostic_item, match_def_path, match_type, paths,
3-
snippet_opt, span_lint_hir, span_lint_hir_and_then, walk_ptrs_ty_depth,
2+
fn_has_unsatisfiable_preds, has_drop, is_copy, is_type_diagnostic_item, match_def_path, paths, snippet_opt,
3+
span_lint_hir, span_lint_hir_and_then, walk_ptrs_ty_depth,
44
};
55
use if_chain::if_chain;
66
use rustc_data_structures::{fx::FxHashMap, transitive_relation::TransitiveRelation};
@@ -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 cx.tcx.is_diagnostic_item(sym::deref_method, pred_fn_def_id);
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -313,25 +313,12 @@ fn is_ty_param_diagnostic_item(cx: &LateContext<'_>, qpath: &QPath<'tcx>, item:
313313
}
314314
}
315315

316-
/// Checks if the first type parameter is a given item.
317-
fn is_ty_param_path(cx: &LateContext<'_>, qpath: &QPath<'tcx>, path: &[&str]) -> Option<&'tcx hir::Ty<'tcx>> {
318-
let ty = get_qpath_generic_tys(qpath).next()?;
319-
320-
if let TyKind::Path(qpath) = &ty.kind {
321-
cx.qpath_res(qpath, ty.hir_id)
322-
.opt_def_id()
323-
.and_then(|id| match_def_path(cx, id, path).then(|| ty))
324-
} else {
325-
None
326-
}
327-
}
328-
329316
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
330317
if is_ty_param_diagnostic_item(cx, qpath, sym::string_type).is_some() {
331318
Some("str")
332-
} else if is_ty_param_path(cx, qpath, &paths::OS_STRING).is_some() {
319+
} else if is_ty_param_diagnostic_item(cx, qpath, sym::OsString).is_some() {
333320
Some("std::ffi::OsStr")
334-
} else if is_ty_param_path(cx, qpath, &paths::PATH_BUF).is_some() {
321+
} else if is_ty_param_diagnostic_item(cx, qpath, sym::PathBuf).is_some() {
335322
Some("std::path::Path")
336323
} else {
337324
None

clippy_utils/src/paths.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ pub const OPTION: [&str; 3] = ["core", "option", "Option"];
8484
pub const OPTION_NONE: [&str; 4] = ["core", "option", "Option", "None"];
8585
pub const OPTION_SOME: [&str; 4] = ["core", "option", "Option", "Some"];
8686
pub const ORD: [&str; 3] = ["core", "cmp", "Ord"];
87-
pub const OS_STRING: [&str; 4] = ["std", "ffi", "os_str", "OsString"];
8887
pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"];
8988
pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"];
9089
pub(super) const PANICKING_PANIC: [&str; 3] = ["core", "panicking", "panic"];
@@ -155,9 +154,7 @@ pub const SYMBOL_TO_IDENT_STRING: [&str; 4] = ["rustc_span", "symbol", "Symbol",
155154
pub const SYM_MODULE: [&str; 3] = ["rustc_span", "symbol", "sym"];
156155
#[cfg(feature = "internal-lints")]
157156
pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"];
158-
pub const TO_OWNED: [&str; 3] = ["alloc", "borrow", "ToOwned"];
159157
pub const TO_OWNED_METHOD: [&str; 4] = ["alloc", "borrow", "ToOwned", "to_owned"];
160-
pub const TO_STRING: [&str; 3] = ["alloc", "string", "ToString"];
161158
pub const TO_STRING_METHOD: [&str; 4] = ["alloc", "string", "ToString", "to_string"];
162159
pub const TRANSMUTE: [&str; 4] = ["core", "intrinsics", "", "transmute"];
163160
pub const TRY_FROM: [&str; 4] = ["core", "convert", "TryFrom", "try_from"];

0 commit comments

Comments
 (0)