Skip to content

Commit b76acc2

Browse files
committed
Use new macro diagnostic items in is_format_macro
1 parent 916e47b commit b76acc2

File tree

2 files changed

+21
-40
lines changed

2 files changed

+21
-40
lines changed

clippy_utils/src/macros.rs

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ 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"];
2721
/// Preferably use the diagnostic item `sym::Borrow` where possible
@@ -43,17 +37,11 @@ pub const DISPLAY_TRAIT: [&str; 3] = ["core", "fmt", "Display"];
4337
pub const DROP: [&str; 3] = ["core", "mem", "drop"];
4438
#[cfg(feature = "internal")]
4539
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"];
5040
pub const EXIT: [&str; 3] = ["std", "process", "exit"];
5141
pub const F32_EPSILON: [&str; 4] = ["core", "f32", "<impl f32>", "EPSILON"];
5242
pub const F64_EPSILON: [&str; 4] = ["core", "f64", "<impl f64>", "EPSILON"];
5343
pub const FILE: [&str; 3] = ["std", "fs", "File"];
5444
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"];
5745
pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"];
5846
pub const FROM_ITERATOR_METHOD: [&str; 6] = ["core", "iter", "traits", "collect", "FromIterator", "from_iter"];
5947
pub const FROM_STR_METHOD: [&str; 5] = ["core", "str", "traits", "FromStr", "from_str"];
@@ -116,10 +104,6 @@ pub const PERMISSIONS_FROM_MODE: [&str; 6] = ["std", "os", "unix", "fs", "Permis
116104
pub const POLL: [&str; 4] = ["core", "task", "poll", "Poll"];
117105
pub const POLL_PENDING: [&str; 5] = ["core", "task", "poll", "Poll", "Pending"];
118106
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"];
123107
pub const PTR_COPY: [&str; 3] = ["core", "intrinsics", "copy"];
124108
pub const PTR_COPY_NONOVERLAPPING: [&str; 3] = ["core", "intrinsics", "copy_nonoverlapping"];
125109
pub const PTR_EQ: [&str; 3] = ["core", "ptr", "eq"];
@@ -200,8 +184,4 @@ pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
200184
pub const VEC_RESIZE: [&str; 4] = ["alloc", "vec", "Vec", "resize"];
201185
pub const WEAK_ARC: [&str; 3] = ["alloc", "sync", "Weak"];
202186
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"];
207187
pub const PTR_NON_NULL: [&str; 4] = ["core", "ptr", "non_null", "NonNull"];

0 commit comments

Comments
 (0)