Skip to content

Commit 4417f78

Browse files
committed
Auto merge of rust-lang#8474 - Alexendoo:paths, r=Manishearth
Replace some more paths with diagnostic items cc rust-lang#5393 Replaces the macro & mem paths, and catches a couple others that were unused changelog: none
2 parents 95897bf + 2955db4 commit 4417f78

10 files changed

+63
-86
lines changed

clippy_lints/src/drop_forget_ref.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_note;
22
use clippy_utils::ty::is_copy;
3-
use clippy_utils::{match_def_path, paths};
43
use if_chain::if_chain;
54
use rustc_hir::{Expr, ExprKind};
65
use rustc_lint::{LateContext, LateLintPass};
76
use rustc_middle::ty;
87
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_span::sym;
99

1010
declare_clippy_lint! {
1111
/// ### What it does
@@ -128,14 +128,16 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
128128
let arg_ty = cx.typeck_results().expr_ty(arg);
129129

130130
if let ty::Ref(..) = arg_ty.kind() {
131-
if match_def_path(cx, def_id, &paths::DROP) {
132-
lint = DROP_REF;
133-
msg = DROP_REF_SUMMARY.to_string();
134-
} else if match_def_path(cx, def_id, &paths::MEM_FORGET) {
135-
lint = FORGET_REF;
136-
msg = FORGET_REF_SUMMARY.to_string();
137-
} else {
138-
return;
131+
match cx.tcx.get_diagnostic_name(def_id) {
132+
Some(sym::mem_drop) => {
133+
lint = DROP_REF;
134+
msg = DROP_REF_SUMMARY.to_string();
135+
},
136+
Some(sym::mem_forget) => {
137+
lint = FORGET_REF;
138+
msg = FORGET_REF_SUMMARY.to_string();
139+
},
140+
_ => return,
139141
}
140142
span_lint_and_note(cx,
141143
lint,
@@ -144,14 +146,16 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
144146
Some(arg.span),
145147
&format!("argument has type `{}`", arg_ty));
146148
} else if is_copy(cx, arg_ty) {
147-
if match_def_path(cx, def_id, &paths::DROP) {
148-
lint = DROP_COPY;
149-
msg = DROP_COPY_SUMMARY.to_string();
150-
} else if match_def_path(cx, def_id, &paths::MEM_FORGET) {
151-
lint = FORGET_COPY;
152-
msg = FORGET_COPY_SUMMARY.to_string();
153-
} else {
154-
return;
149+
match cx.tcx.get_diagnostic_name(def_id) {
150+
Some(sym::mem_drop) => {
151+
lint = DROP_COPY;
152+
msg = DROP_COPY_SUMMARY.to_string();
153+
},
154+
Some(sym::mem_forget) => {
155+
lint = FORGET_COPY;
156+
msg = FORGET_COPY_SUMMARY.to_string();
157+
},
158+
_ => return,
155159
}
156160
span_lint_and_note(cx,
157161
lint,

clippy_lints/src/manual_bits.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_opt;
3-
use clippy_utils::{match_def_path, meets_msrv, msrvs, paths};
3+
use clippy_utils::{meets_msrv, msrvs};
44
use rustc_ast::ast::LitKind;
55
use rustc_errors::Applicability;
66
use rustc_hir::{BinOpKind, Expr, ExprKind, GenericArg, QPath};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::ty::{self, Ty};
99
use rustc_semver::RustcVersion;
1010
use rustc_session::{declare_tool_lint, impl_lint_pass};
11+
use rustc_span::sym;
1112

1213
declare_clippy_lint! {
1314
/// ### What it does
@@ -99,7 +100,7 @@ fn get_size_of_ty<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<
99100
if let Some(GenericArg::Type(real_ty)) = args.args.get(0);
100101

101102
if let Some(def_id) = cx.qpath_res(count_func_qpath, count_func.hir_id).opt_def_id();
102-
if match_def_path(cx, def_id, &paths::MEM_SIZE_OF);
103+
if cx.tcx.is_diagnostic_item(sym::mem_size_of, def_id);
103104
then {
104105
cx.typeck_results().node_substs(count_func.hir_id).types().next().map(|resolved_ty| (real_ty, resolved_ty))
105106
} else {

clippy_lints/src/mem_forget.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::{match_def_path, paths};
32
use rustc_hir::{Expr, ExprKind};
43
use rustc_lint::{LateContext, LateLintPass};
54
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
use rustc_span::sym;
66

77
declare_clippy_lint! {
88
/// ### What it does
@@ -32,7 +32,7 @@ impl<'tcx> LateLintPass<'tcx> for MemForget {
3232
if let ExprKind::Call(path_expr, [ref first_arg, ..]) = e.kind {
3333
if let ExprKind::Path(ref qpath) = path_expr.kind {
3434
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
35-
if match_def_path(cx, def_id, &paths::MEM_FORGET) {
35+
if cx.tcx.is_diagnostic_item(sym::mem_forget, def_id) {
3636
let forgot_ty = cx.typeck_results().expr_ty(first_arg);
3737

3838
if forgot_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {

clippy_lints/src/mem_replace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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};
33
use clippy_utils::ty::is_non_aggregate_primitive_type;
4-
use clippy_utils::{is_default_equivalent, is_lang_ctor, match_def_path, meets_msrv, msrvs, paths};
4+
use clippy_utils::{is_default_equivalent, is_lang_ctor, meets_msrv, msrvs};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::LangItem::OptionNone;
@@ -249,7 +249,7 @@ impl<'tcx> LateLintPass<'tcx> for MemReplace {
249249
if let ExprKind::Call(func, func_args) = expr.kind;
250250
if let ExprKind::Path(ref func_qpath) = func.kind;
251251
if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
252-
if match_def_path(cx, def_id, &paths::MEM_REPLACE);
252+
if cx.tcx.is_diagnostic_item(sym::mem_replace, def_id);
253253
if let [dest, src] = func_args;
254254
then {
255255
check_replace_option_with_none(cx, src, dest, expr.span);

clippy_lints/src/methods/uninit_assumed_init.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::{is_expr_path_def_path, paths, ty::is_uninit_value_valid_for_ty};
2+
use clippy_utils::{is_expr_diagnostic_item, ty::is_uninit_value_valid_for_ty};
33
use if_chain::if_chain;
44
use rustc_hir as hir;
55
use rustc_lint::LateContext;
6+
use rustc_span::sym;
67

78
use super::UNINIT_ASSUMED_INIT;
89

@@ -11,7 +12,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
1112
if_chain! {
1213
if let hir::ExprKind::Call(callee, args) = recv.kind;
1314
if args.is_empty();
14-
if is_expr_path_def_path(cx, callee, &paths::MEM_MAYBEUNINIT_UNINIT);
15+
if is_expr_diagnostic_item(cx, callee, sym::maybe_uninit_uninit);
1516
if !is_uninit_value_valid_for_ty(cx, cx.typeck_results().expr_ty_adjusted(expr));
1617
then {
1718
span_lint(

clippy_lints/src/redundant_clone.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
135135
}
136136

137137
if let ty::Adt(def, _) = arg_ty.kind() {
138-
if match_def_path(cx, def.did, &paths::MEM_MANUALLY_DROP) {
138+
if def.is_manually_drop() {
139139
continue;
140140
}
141141
}

clippy_lints/src/size_of_in_element_count.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_hir::{Expr, ExprKind};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty::{self, Ty, TypeAndMut};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
12+
use rustc_span::sym;
1213

1314
declare_clippy_lint! {
1415
/// ### What it does
@@ -44,8 +45,7 @@ fn get_size_of_ty<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, inverted:
4445
if !inverted;
4546
if let ExprKind::Path(ref count_func_qpath) = count_func.kind;
4647
if let Some(def_id) = cx.qpath_res(count_func_qpath, count_func.hir_id).opt_def_id();
47-
if match_def_path(cx, def_id, &paths::MEM_SIZE_OF)
48-
|| match_def_path(cx, def_id, &paths::MEM_SIZE_OF_VAL);
48+
if matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::mem_size_of | sym::mem_size_of_val));
4949
then {
5050
cx.typeck_results().node_substs(count_func.hir_id).types().next()
5151
} else {

clippy_lints/src/undropped_manually_drops.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::path_res;
23
use clippy_utils::ty::is_type_lang_item;
3-
use clippy_utils::{match_function_call, paths};
4-
use rustc_hir::{lang_items, Expr};
4+
use rustc_hir::{lang_items, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::sym;
78

89
declare_clippy_lint! {
910
/// ### What it does
@@ -38,9 +39,12 @@ declare_lint_pass!(UndroppedManuallyDrops => [UNDROPPED_MANUALLY_DROPS]);
3839

3940
impl<'tcx> LateLintPass<'tcx> for UndroppedManuallyDrops {
4041
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
41-
if let Some([arg_0, ..]) = match_function_call(cx, expr, &paths::DROP) {
42+
if_chain! {
43+
if let ExprKind::Call(fun, [arg_0, ..]) = expr.kind;
44+
if path_res(cx, fun).opt_def_id() == cx.tcx.get_diagnostic_item(sym::mem_drop);
4245
let ty = cx.typeck_results().expr_ty(arg_0);
43-
if is_type_lang_item(cx, ty, lang_items::LangItem::ManuallyDrop) {
46+
if is_type_lang_item(cx, ty, lang_items::LangItem::ManuallyDrop);
47+
then {
4448
span_lint_and_help(
4549
cx,
4650
UNDROPPED_MANUALLY_DROPS,

clippy_utils/src/macros.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![allow(clippy::similar_names)] // `expr` and `expn`
22

33
use crate::visitors::expr_visitor_no_bodies;
4-
use crate::{match_def_path, paths};
54

65
use arrayvec::ArrayVec;
76
use if_chain::if_chain;
@@ -14,29 +13,31 @@ use rustc_span::hygiene::{self, MacroKind, SyntaxContext};
1413
use rustc_span::{sym, ExpnData, ExpnId, ExpnKind, Span, Symbol};
1514
use std::ops::ControlFlow;
1615

17-
const FORMAT_MACRO_PATHS: &[&[&str]] = &[
18-
&paths::FORMAT_ARGS_MACRO,
19-
&paths::ASSERT_EQ_MACRO,
20-
&paths::ASSERT_MACRO,
21-
&paths::ASSERT_NE_MACRO,
22-
&paths::EPRINT_MACRO,
23-
&paths::EPRINTLN_MACRO,
24-
&paths::PRINT_MACRO,
25-
&paths::PRINTLN_MACRO,
26-
&paths::WRITE_MACRO,
27-
&paths::WRITELN_MACRO,
16+
const FORMAT_MACRO_DIAG_ITEMS: &[Symbol] = &[
17+
sym::assert_eq_macro,
18+
sym::assert_macro,
19+
sym::assert_ne_macro,
20+
sym::debug_assert_eq_macro,
21+
sym::debug_assert_macro,
22+
sym::debug_assert_ne_macro,
23+
sym::eprint_macro,
24+
sym::eprintln_macro,
25+
sym::format_args_macro,
26+
sym::format_macro,
27+
sym::print_macro,
28+
sym::println_macro,
29+
sym::std_panic_macro,
30+
sym::write_macro,
31+
sym::writeln_macro,
2832
];
2933

30-
const FORMAT_MACRO_DIAG_ITEMS: &[Symbol] = &[sym::format_macro, sym::std_panic_macro];
31-
3234
/// Returns true if a given Macro `DefId` is a format macro (e.g. `println!`)
3335
pub fn is_format_macro(cx: &LateContext<'_>, macro_def_id: DefId) -> bool {
34-
FORMAT_MACRO_PATHS
35-
.iter()
36-
.any(|path| match_def_path(cx, macro_def_id, path))
37-
|| FORMAT_MACRO_DIAG_ITEMS
38-
.iter()
39-
.any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, macro_def_id))
36+
if let Some(name) = cx.tcx.get_diagnostic_name(macro_def_id) {
37+
FORMAT_MACRO_DIAG_ITEMS.contains(&name)
38+
} else {
39+
false
40+
}
4041
}
4142

4243
/// A macro call, like `vec![1, 2, 3]`.

clippy_utils/src/paths.rs

-34
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,27 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [
1616
#[cfg(feature = "internal")]
1717
pub const DIAGNOSTIC_BUILDER: [&str; 3] = ["rustc_errors", "diagnostic_builder", "DiagnosticBuilder"];
1818
pub const ARC_PTR_EQ: [&str; 4] = ["alloc", "sync", "Arc", "ptr_eq"];
19-
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
20-
pub const ASSERT_EQ_MACRO: [&str; 3] = ["core", "macros", "assert_eq"];
21-
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
22-
pub const ASSERT_MACRO: [&str; 4] = ["core", "macros", "builtin", "assert"];
23-
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
24-
pub const ASSERT_NE_MACRO: [&str; 3] = ["core", "macros", "assert_ne"];
2519
pub const ASMUT_TRAIT: [&str; 3] = ["core", "convert", "AsMut"];
2620
pub const ASREF_TRAIT: [&str; 3] = ["core", "convert", "AsRef"];
27-
/// Preferably use the diagnostic item `sym::Borrow` where possible
28-
pub const BORROW_TRAIT: [&str; 3] = ["core", "borrow", "Borrow"];
29-
pub const BORROW_MUT_TRAIT: [&str; 3] = ["core", "borrow", "BorrowMut"];
3021
pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"];
3122
pub const BTREEMAP_ENTRY: [&str; 6] = ["alloc", "collections", "btree", "map", "entry", "Entry"];
3223
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
3324
pub const CLONE_TRAIT_METHOD: [&str; 4] = ["core", "clone", "Clone", "clone"];
3425
pub const COW: [&str; 3] = ["alloc", "borrow", "Cow"];
3526
pub const CSTRING_AS_C_STR: [&str; 5] = ["std", "ffi", "c_str", "CString", "as_c_str"];
36-
pub const DEBUG_TRAIT: [&str; 3] = ["core", "fmt", "Debug"];
3727
pub const DEFAULT_TRAIT_METHOD: [&str; 4] = ["core", "default", "Default", "default"];
3828
pub const DEREF_MUT_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "DerefMut", "deref_mut"];
3929
/// Preferably use the diagnostic item `sym::deref_method` where possible
4030
pub const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", "deref"];
4131
pub const DIR_BUILDER: [&str; 3] = ["std", "fs", "DirBuilder"];
4232
pub const DISPLAY_TRAIT: [&str; 3] = ["core", "fmt", "Display"];
43-
pub const DROP: [&str; 3] = ["core", "mem", "drop"];
4433
#[cfg(feature = "internal")]
4534
pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"];
46-
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
47-
pub const EPRINT_MACRO: [&str; 3] = ["std", "macros", "eprint"];
48-
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
49-
pub const EPRINTLN_MACRO: [&str; 3] = ["std", "macros", "eprintln"];
5035
pub const EXIT: [&str; 3] = ["std", "process", "exit"];
5136
pub const F32_EPSILON: [&str; 4] = ["core", "f32", "<impl f32>", "EPSILON"];
5237
pub const F64_EPSILON: [&str; 4] = ["core", "f64", "<impl f64>", "EPSILON"];
5338
pub const FILE: [&str; 3] = ["std", "fs", "File"];
5439
pub const FILE_TYPE: [&str; 3] = ["std", "fs", "FileType"];
55-
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
56-
pub const FORMAT_ARGS_MACRO: [&str; 4] = ["core", "macros", "builtin", "format_args"];
5740
pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"];
5841
pub const FROM_ITERATOR_METHOD: [&str; 6] = ["core", "iter", "traits", "collect", "FromIterator", "from_iter"];
5942
pub const FROM_STR_METHOD: [&str; 5] = ["core", "str", "traits", "FromStr", "from_str"];
@@ -85,17 +68,8 @@ pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"];
8568
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
8669
#[cfg(feature = "internal")]
8770
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];
88-
pub const MEM_DISCRIMINANT: [&str; 3] = ["core", "mem", "discriminant"];
89-
pub const MEM_FORGET: [&str; 3] = ["core", "mem", "forget"];
90-
pub const MEM_MANUALLY_DROP: [&str; 4] = ["core", "mem", "manually_drop", "ManuallyDrop"];
91-
pub const MEM_MAYBEUNINIT: [&str; 4] = ["core", "mem", "maybe_uninit", "MaybeUninit"];
92-
pub const MEM_MAYBEUNINIT_UNINIT: [&str; 5] = ["core", "mem", "maybe_uninit", "MaybeUninit", "uninit"];
93-
pub const MEM_REPLACE: [&str; 3] = ["core", "mem", "replace"];
94-
pub const MEM_SIZE_OF: [&str; 3] = ["core", "mem", "size_of"];
95-
pub const MEM_SIZE_OF_VAL: [&str; 3] = ["core", "mem", "size_of_val"];
9671
pub const MUTEX_GUARD: [&str; 4] = ["std", "sync", "mutex", "MutexGuard"];
9772
pub const OPEN_OPTIONS: [&str; 3] = ["std", "fs", "OpenOptions"];
98-
pub const OPS_MODULE: [&str; 2] = ["core", "ops"];
9973
/// Preferably use the diagnostic item `sym::Option` where possible
10074
pub const OPTION: [&str; 3] = ["core", "option", "Option"];
10175
pub const OPTION_NONE: [&str; 4] = ["core", "option", "Option", "None"];
@@ -116,10 +90,6 @@ pub const PERMISSIONS_FROM_MODE: [&str; 6] = ["std", "os", "unix", "fs", "Permis
11690
pub const POLL: [&str; 4] = ["core", "task", "poll", "Poll"];
11791
pub const POLL_PENDING: [&str; 5] = ["core", "task", "poll", "Poll", "Pending"];
11892
pub const POLL_READY: [&str; 5] = ["core", "task", "poll", "Poll", "Ready"];
119-
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
120-
pub const PRINT_MACRO: [&str; 3] = ["std", "macros", "print"];
121-
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
122-
pub const PRINTLN_MACRO: [&str; 3] = ["std", "macros", "println"];
12393
pub const PTR_COPY: [&str; 3] = ["core", "intrinsics", "copy"];
12494
pub const PTR_COPY_NONOVERLAPPING: [&str; 3] = ["core", "intrinsics", "copy_nonoverlapping"];
12595
pub const PTR_EQ: [&str; 3] = ["core", "ptr", "eq"];
@@ -200,8 +170,4 @@ pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
200170
pub const VEC_RESIZE: [&str; 4] = ["alloc", "vec", "Vec", "resize"];
201171
pub const WEAK_ARC: [&str; 3] = ["alloc", "sync", "Weak"];
202172
pub const WEAK_RC: [&str; 3] = ["alloc", "rc", "Weak"];
203-
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
204-
pub const WRITE_MACRO: [&str; 3] = ["core", "macros", "write"];
205-
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
206-
pub const WRITELN_MACRO: [&str; 3] = ["core", "macros", "writeln"];
207173
pub const PTR_NON_NULL: [&str; 4] = ["core", "ptr", "non_null", "NonNull"];

0 commit comments

Comments
 (0)