Skip to content

Commit b74796d

Browse files
committed
how to compare macro to import path
1 parent 2c154f2 commit b74796d

File tree

5 files changed

+24
-50
lines changed

5 files changed

+24
-50
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10141014
store.register_early_pass(move || box excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools));
10151015
store.register_early_pass(|| box option_env_unwrap::OptionEnvUnwrap);
10161016
store.register_late_pass(|| box wildcard_imports::WildcardImports);
1017-
store.register_early_pass(|| box macro_use::MacroUseImports);
1017+
store.register_early_pass(|| box macro_use::MacroUseImports::default());
10181018

10191019
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
10201020
LintId::of(&arithmetic::FLOAT_ARITHMETIC),

clippy_lints/src/macro_use.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,21 @@ declare_clippy_lint! {
5050
/// #[macro_use]
5151
/// use lazy_static;
5252
/// ```
53-
pub MACRO_USE_IMPORT,
53+
pub MACRO_USE_IMPORTS,
5454
pedantic,
5555
"#[macro_use] is no longer needed"
5656
}
5757

5858
#[derive(Default)]
59-
pub struct MacroUseImport {
59+
pub struct MacroUseImports {
6060
/// the actual import path used and its span.
6161
imports: Vec<(String, Span)>,
6262
/// the span of the macro reference and the `MacroRefData`
6363
/// for the use of the macro.
6464
collected: FxHashMap<Span, MacroRefData>,
6565
}
6666

67-
impl MacroUseImport {
67+
impl MacroUseImports {
6868
fn import_path_mac(&self, use_path: &str) -> String {
6969
println!("END {:?}", use_path);
7070

@@ -79,12 +79,16 @@ impl MacroUseImport {
7979

8080
fn paths_match(mac: &MacroRefData, use_path: &str) -> bool {
8181
let segs = mac.path.split("::")
82-
.filter(|s| *s == "")
82+
.filter(|s| *s != "")
8383
.collect::<Vec<_>>();
84+
85+
println!("{:?}", segs);
86+
8487
if segs.starts_with(&["std"]) {
8588
return PRELUDE.iter().any(|m| segs.contains(m))
8689
}
87-
false
90+
91+
segs.starts_with(&use_path.split("::").collect::<Vec<_>>())
8892
}
8993

9094
fn make_path(mac: &MacroRefData, use_path: &str) -> String {
@@ -98,9 +102,9 @@ fn make_path(mac: &MacroRefData, use_path: &str) -> String {
98102
res
99103
}
100104

101-
impl_lint_pass!(MacroUseImport => [MACRO_USE_IMPORT]);
105+
impl_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]);
102106

103-
impl EarlyLintPass for MacroUseImport {
107+
impl EarlyLintPass for MacroUseImports {
104108
fn check_item(&mut self, ecx: &EarlyContext<'_>, item: &ast::Item) {
105109
if_chain! {
106110
if ecx.sess.opts.edition == Edition::Edition2018;
@@ -175,7 +179,7 @@ impl EarlyLintPass for MacroUseImport {
175179
let help = format!("use {}", import_path);
176180
span_lint_and_sugg(
177181
ecx,
178-
MACRO_USE_IMPORT,
182+
MACRO_USE_IMPORTS,
179183
*span,
180184
msg,
181185
// "remove the attribute and import the macro directly, try",

tests/ui/macro_use_import.rs

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/ui/macro_use_import.stderr

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/ui/macro_use_imports.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
// compile-flags: --edition 2018
22
#![warn(clippy::macro_use_imports)]
3+
#![feature(rustc_private)]
34

45
use std::collections::HashMap;
56
#[macro_use]
6-
use std::prelude;
7+
use std::{prelude, sync::Mutex};
8+
#[macro_use]
9+
extern crate rustc_session;
710

811
fn main() {
12+
let _ = Mutex::new(8_u8);
913
let _ = HashMap::<u8, u8>::new();
1014
println!();
15+
declare_tool_lint! {
16+
pub clippy::TEST_LINT,
17+
Warn,
18+
"",
19+
report_in_external_macro: true
20+
}
1121
}

0 commit comments

Comments
 (0)