Skip to content

Commit d02ca3b

Browse files
committed
Auto merge of #6807 - anall:feature/use_new_diagnostics, r=Manishearth
migrate paths to newly-added diagnostic items This gets rid of the following paths: * OS_STRING * TO_OWNED * TO_STRING Removes some usages of: * PATH_BUF Per #5393 also removes unneeded `is_ty_param_path` from `clippy_lints::types` and relocates `is_ty_param_lang_item` and `is_ty_param_diagnostic_item` to `clippy_utils`. changelog: none
2 parents e451d6e + 9bdc273 commit d02ca3b

File tree

8 files changed

+64
-67
lines changed

8 files changed

+64
-67
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

+4-4
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

+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

+7-46
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, Symbol};
26+
use rustc_span::symbol::sym;
2727
use rustc_target::abi::LayoutOf;
2828
use rustc_target::spec::abi::Abi;
2929
use rustc_typeck::hir_ty_to_ty;
@@ -33,10 +33,10 @@ use crate::utils::paths;
3333
use crate::utils::sugg::Sugg;
3434
use crate::utils::{
3535
clip, comparisons, differing_macro_contexts, get_qpath_generic_tys, higher, in_constant, indent_of, int_bits,
36-
is_hir_ty_cfg_dependant, is_type_diagnostic_item, last_path_segment, match_def_path, match_path, meets_msrv,
37-
method_chain_args, multispan_sugg, numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt,
38-
snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg,
39-
span_lint_and_then, unsext,
36+
is_hir_ty_cfg_dependant, is_ty_param_diagnostic_item, is_ty_param_lang_item, is_type_diagnostic_item,
37+
last_path_segment, match_def_path, match_path, meets_msrv, method_chain_args, multispan_sugg,
38+
numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt, snippet_with_applicability,
39+
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext,
4040
};
4141

4242
declare_clippy_lint! {
@@ -287,51 +287,12 @@ impl<'tcx> LateLintPass<'tcx> for Types {
287287
}
288288
}
289289

290-
/// Checks if the first type parameter is a lang item.
291-
fn is_ty_param_lang_item(cx: &LateContext<'_>, qpath: &QPath<'tcx>, item: LangItem) -> Option<&'tcx hir::Ty<'tcx>> {
292-
let ty = get_qpath_generic_tys(qpath).next()?;
293-
294-
if let TyKind::Path(qpath) = &ty.kind {
295-
cx.qpath_res(qpath, ty.hir_id)
296-
.opt_def_id()
297-
.and_then(|id| (cx.tcx.lang_items().require(item) == Ok(id)).then(|| ty))
298-
} else {
299-
None
300-
}
301-
}
302-
303-
/// Checks if the first type parameter is a diagnostic item.
304-
fn is_ty_param_diagnostic_item(cx: &LateContext<'_>, qpath: &QPath<'tcx>, item: Symbol) -> Option<&'tcx hir::Ty<'tcx>> {
305-
let ty = get_qpath_generic_tys(qpath).next()?;
306-
307-
if let TyKind::Path(qpath) = &ty.kind {
308-
cx.qpath_res(qpath, ty.hir_id)
309-
.opt_def_id()
310-
.and_then(|id| cx.tcx.is_diagnostic_item(item, id).then(|| ty))
311-
} else {
312-
None
313-
}
314-
}
315-
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-
329290
fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
330291
if is_ty_param_diagnostic_item(cx, qpath, sym::string_type).is_some() {
331292
Some("str")
332-
} else if is_ty_param_path(cx, qpath, &paths::OS_STRING).is_some() {
293+
} else if is_ty_param_diagnostic_item(cx, qpath, sym::OsString).is_some() {
333294
Some("std::ffi::OsStr")
334-
} else if is_ty_param_path(cx, qpath, &paths::PATH_BUF).is_some() {
295+
} else if is_ty_param_diagnostic_item(cx, qpath, sym::PathBuf).is_some() {
335296
Some("std::path::Path")
336297
} else {
337298
None

clippy_utils/src/lib.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
6464
use rustc_hir::Node;
6565
use rustc_hir::{
6666
def, Arm, Block, Body, Constness, Crate, Expr, ExprKind, FnDecl, GenericArgs, HirId, Impl, ImplItem, ImplItemKind,
67-
Item, ItemKind, MatchSource, Param, Pat, PatKind, Path, PathSegment, QPath, TraitItem, TraitItemKind, TraitRef,
68-
TyKind, Unsafety,
67+
Item, ItemKind, LangItem, MatchSource, Param, Pat, PatKind, Path, PathSegment, QPath, TraitItem, TraitItemKind,
68+
TraitRef, TyKind, Unsafety,
6969
};
7070
use rustc_infer::infer::TyCtxtInferExt;
7171
use rustc_lint::{LateContext, Level, Lint, LintContext};
@@ -232,6 +232,36 @@ pub fn is_type_lang_item(cx: &LateContext<'_>, ty: Ty<'_>, lang_item: hir::LangI
232232
}
233233
}
234234

235+
/// Checks if the first type parameter is a lang item.
236+
pub fn is_ty_param_lang_item(cx: &LateContext<'_>, qpath: &QPath<'tcx>, item: LangItem) -> Option<&'tcx hir::Ty<'tcx>> {
237+
let ty = get_qpath_generic_tys(qpath).next()?;
238+
239+
if let TyKind::Path(qpath) = &ty.kind {
240+
cx.qpath_res(qpath, ty.hir_id)
241+
.opt_def_id()
242+
.and_then(|id| (cx.tcx.lang_items().require(item) == Ok(id)).then(|| ty))
243+
} else {
244+
None
245+
}
246+
}
247+
248+
/// Checks if the first type parameter is a diagnostic item.
249+
pub fn is_ty_param_diagnostic_item(
250+
cx: &LateContext<'_>,
251+
qpath: &QPath<'tcx>,
252+
item: Symbol,
253+
) -> Option<&'tcx hir::Ty<'tcx>> {
254+
let ty = get_qpath_generic_tys(qpath).next()?;
255+
256+
if let TyKind::Path(qpath) = &ty.kind {
257+
cx.qpath_res(qpath, ty.hir_id)
258+
.opt_def_id()
259+
.and_then(|id| cx.tcx.is_diagnostic_item(item, id).then(|| ty))
260+
} else {
261+
None
262+
}
263+
}
264+
235265
/// Checks if the method call given in `expr` belongs to the given trait.
236266
pub fn match_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, path: &[&str]) -> bool {
237267
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();

clippy_utils/src/paths.rs

-3
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)