Skip to content

Commit 41abea0

Browse files
committed
a bit of clean up
1 parent 94b13b0 commit 41abea0

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

clippy_lints/src/macro_use.rs

+26-23
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ use rustc_session::{declare_tool_lint, impl_lint_pass};
99
use rustc_span::{edition::Edition, Span};
1010
use rustc_ast::ast;
1111

12-
const PRELUDE: &[&str] = &[
13-
"marker", "ops", "convert", "iter", "option", "result", "borrow", "boxed", "string", "vec", "macros",
14-
];
15-
const BRACKETS: &[char] = &['<', '>'];
16-
1712
declare_clippy_lint! {
1813
/// **What it does:** Checks for `#[macro_use] use...`.
1914
///
@@ -32,6 +27,8 @@ declare_clippy_lint! {
3227
"#[macro_use] is no longer needed"
3328
}
3429

30+
const BRACKETS: &[char] = &['<', '>'];
31+
3532
/// MacroRefData includes the name of the macro
3633
/// and the path from `SourceMap::span_to_filename`.
3734
#[derive(Debug, Clone)]
@@ -61,30 +58,15 @@ impl MacroRefData {
6158

6259
#[derive(Default)]
6360
pub struct MacroUseImports {
64-
/// the actual import path used and its span.
61+
/// the actual import path used and the span of the attribute above it.
6562
imports: Vec<(String, Span)>,
6663
/// the span of the macro reference and the `MacroRefData`
6764
/// for the use of the macro.
65+
/// TODO make this FxHashSet<Span> to guard against inserting already found macros
6866
collected: FxHashMap<Span, MacroRefData>,
6967
mac_refs: Vec<(Span, MacroRefData)>,
7068
}
7169

72-
/// This is somewhat of a fallback for imports from `std::prelude` because they
73-
/// are not recognized by `LateLintPass::check_item` `lcx.tcx.item_children(id)`
74-
fn make_path(mac: &MacroRefData, use_path: &str) -> String {
75-
let segs = mac.path.split("::").filter(|s| *s != "").collect::<Vec<_>>();
76-
77-
if segs.starts_with(&["std"]) && PRELUDE.iter().any(|m| segs.contains(m)) {
78-
return format!("std::prelude::{} is imported by default, remove `use` statement", mac.name);
79-
}
80-
81-
if use_path.split("::").count() == 1 {
82-
return format!("{}::{}", use_path, mac.name);
83-
}
84-
85-
mac.path.clone()
86-
}
87-
8870
impl_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]);
8971

9072
impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
@@ -233,9 +215,30 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
233215
}
234216
}
235217
if !self.mac_refs.is_empty() {
236-
// TODO if not empty we found one would could not make a suggestion for
218+
// TODO if not empty we found one we could not make a suggestion for
237219
// such as std::prelude::v1 or something else I haven't thought of.
238220
// println!("{:#?}", self.mac_refs);
239221
}
240222
}
241223
}
224+
225+
226+
const PRELUDE: &[&str] = &[
227+
"marker", "ops", "convert", "iter", "option", "result", "borrow", "boxed", "string", "vec", "macros",
228+
];
229+
230+
/// This is somewhat of a fallback for imports from `std::prelude` because they
231+
/// are not recognized by `LateLintPass::check_item` `lcx.tcx.item_children(id)`
232+
fn make_path(mac: &MacroRefData, use_path: &str) -> String {
233+
let segs = mac.path.split("::").filter(|s| *s != "").collect::<Vec<_>>();
234+
235+
if segs.starts_with(&["std"]) && PRELUDE.iter().any(|m| segs.contains(m)) {
236+
return format!("std::prelude::{} is imported by default, remove `use` statement", mac.name);
237+
}
238+
239+
if use_path.split("::").count() == 1 {
240+
return format!("{}::{}", use_path, mac.name);
241+
}
242+
243+
mac.path.clone()
244+
}

0 commit comments

Comments
 (0)