Skip to content

Commit

Permalink
remove stdout, fix clippy warnings, fmtcar
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinR528 committed Apr 16, 2020
1 parent 1ff58e7 commit ab2b14b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 69 deletions.
79 changes: 27 additions & 52 deletions clippy_lints/src/macro_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ declare_clippy_lint! {

const BRACKETS: &[char] = &['<', '>'];

/// MacroRefData includes the name of the macro
/// `MacroRefData` includes the name of the macro
/// and the path from `SourceMap::span_to_filename`.
#[derive(Debug, Clone)]
pub struct MacroRefData {
Expand All @@ -38,7 +38,7 @@ pub struct MacroRefData {
}

impl MacroRefData {
pub fn new(name: String, span: Span, ecx: &LateContext<'_, '_>) -> Self {
pub fn new(name: &str, span: Span, ecx: &LateContext<'_, '_>) -> Self {
let mut path = ecx.sess().source_map().span_to_filename(span).to_string();

// std lib paths are <::std::module::file type>
Expand All @@ -57,6 +57,7 @@ impl MacroRefData {
}

#[derive(Default)]
#[allow(clippy::module_name_repetitions)]
pub struct MacroUseImports {
/// the actual import path used and the span of the attribute above it.
imports: Vec<(String, Span)>,
Expand All @@ -70,27 +71,27 @@ impl_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]);
impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
fn check_item(&mut self, lcx: &LateContext<'_, '_>, item: &hir::Item<'_>) {
if_chain! {
if lcx.sess().opts.edition == Edition::Edition2018;
if let hir::ItemKind::Use(path, _kind) = &item.kind;
if let Some(mac_attr) = item
.attrs
.iter()
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
if let Res::Def(DefKind::Mod, id) = path.res;
then {
for kid in lcx.tcx.item_children(id).iter() {
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res {
let span = mac_attr.span.clone();
self.imports.push((lcx.tcx.def_path_str(mac_id), span));
if lcx.sess().opts.edition == Edition::Edition2018;
if let hir::ItemKind::Use(path, _kind) = &item.kind;
if let Some(mac_attr) = item
.attrs
.iter()
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
if let Res::Def(DefKind::Mod, id) = path.res;
then {
for kid in lcx.tcx.item_children(id).iter() {
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res {
let span = mac_attr.span;
self.imports.push((lcx.tcx.def_path_str(mac_id), span));
}
}
}
} else {
} else {
if in_macro(item.span) {
let call_site = item.span.source_callsite();
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
if let Some(callee) = item.span.source_callee() {
if !self.collected.contains(&call_site) {
self.mac_refs.push((call_site, MacroRefData::new(name.into(), callee.def_site, lcx)));
self.mac_refs.push((call_site, MacroRefData::new(&name, callee.def_site, lcx)));
self.collected.insert(call_site);
}
}
Expand All @@ -111,7 +112,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
};

self.mac_refs
.push((call_site, MacroRefData::new(name, callee.def_site, lcx)));
.push((call_site, MacroRefData::new(&name, callee.def_site, lcx)));
self.collected.insert(call_site);
}
}
Expand All @@ -130,7 +131,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
};

self.mac_refs
.push((call_site, MacroRefData::new(name, callee.def_site, lcx)));
.push((call_site, MacroRefData::new(&name, callee.def_site, lcx)));
self.collected.insert(call_site);
}
}
Expand All @@ -149,7 +150,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
};

self.mac_refs
.push((call_site, MacroRefData::new(name, callee.def_site, lcx)));
.push((call_site, MacroRefData::new(&name, callee.def_site, lcx)));
self.collected.insert(call_site);
}
}
Expand All @@ -162,7 +163,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
if let Some(callee) = pat.span.source_callee() {
if !self.collected.contains(&call_site) {
self.mac_refs
.push((call_site, MacroRefData::new(name.to_string(), callee.def_site, lcx)));
.push((call_site, MacroRefData::new(&name, callee.def_site, lcx)));
self.collected.insert(call_site);
}
}
Expand All @@ -175,20 +176,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
if let Some(callee) = ty.span.source_callee() {
if !self.collected.contains(&call_site) {
self.mac_refs
.push((call_site, MacroRefData::new(name.to_string(), callee.def_site, lcx)));
.push((call_site, MacroRefData::new(&name, callee.def_site, lcx)));
self.collected.insert(call_site);
}
}
}
}

fn check_crate_post(&mut self, lcx: &LateContext<'_, '_>, _krate: &hir::Crate<'_>) {
for (import, span) in self.imports.iter() {
let matched = self
.mac_refs
.iter()
.find(|(_span, mac)| import.ends_with(&mac.name))
.is_some();
for (import, span) in &self.imports {
let matched = self.mac_refs.iter().any(|(_span, mac)| import.ends_with(&mac.name));

if matched {
self.mac_refs.retain(|(_span, mac)| !import.ends_with(&mac.name));
Expand All @@ -208,30 +205,8 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
if !self.mac_refs.is_empty() {
// TODO if not empty we found one we could not make a suggestion for
// such as std::prelude::v1 or something else I haven't thought of.
// println!("{:#?}", self.mac_refs);
// If we defer the calling of span_lint_and_sugg we can make a decision about its
// applicability?
}
}
}

const PRELUDE: &[&str] = &[
"marker", "ops", "convert", "iter", "option", "result", "borrow", "boxed", "string", "vec", "macros",
];

/// This is somewhat of a fallback for imports from `std::prelude` because they
/// are not recognized by `LateLintPass::check_item` `lcx.tcx.item_children(id)`
fn make_path(mac: &MacroRefData, use_path: &str) -> String {
let segs = mac.path.split("::").filter(|s| *s != "").collect::<Vec<_>>();

if segs.starts_with(&["std"]) && PRELUDE.iter().any(|m| segs.contains(m)) {
return format!(
"std::prelude::{} is imported by default, remove `use` statement",
mac.name
);
}

if use_path.split("::").count() == 1 {
return format!("{}::{}", use_path, mac.name);
}

mac.path.clone()
}
8 changes: 4 additions & 4 deletions tests/ui/auxiliary/macro_use_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod inner {

// ITEM
#[macro_export]
macro_rules! inner_mod {
macro_rules! inner_mod_macro {
() => {
#[allow(dead_code)]
pub struct Tardis;
Expand All @@ -27,7 +27,7 @@ pub mod inner {

// EXPR
#[macro_export]
macro_rules! function {
macro_rules! function_macro {
() => {
if true {
} else {
Expand All @@ -37,7 +37,7 @@ macro_rules! function {

// TYPE
#[macro_export]
macro_rules! ty_mac {
macro_rules! ty_macro {
() => {
Vec<u8>
};
Expand All @@ -46,7 +46,7 @@ macro_rules! ty_mac {
mod extern_exports {
pub(super) mod private_inner {
#[macro_export]
macro_rules! pub_in_private {
macro_rules! pub_in_private_macro {
($name:ident) => {
let $name = String::from("secrets and lies");
};
Expand Down
Empty file removed tests/ui/macro_use_import.stdout
Empty file.
14 changes: 5 additions & 9 deletions tests/ui/macro_use_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ extern crate macro_use_helper as mac;
extern crate clippy_mini_macro_test as mini_mac;

mod a {
#[macro_use]
use std::prelude;
#[macro_use]
use mac;
#[macro_use]
Expand All @@ -26,15 +24,13 @@ mod a {

fn main() {
pub_macro!();
inner_mod!();
pub_in_private!(_var);
function!();
let v: ty_mac!() = Vec::default();
inner_mod_macro!();
pub_in_private_macro!(_var);
function_macro!();
let v: ty_macro!() = Vec::default();

inner::try_err!();
}
}

fn main() {
println!();
}
fn main() {}
44 changes: 40 additions & 4 deletions tests/ui/macro_use_imports.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
--> $DIR/macro_use_imports.rs:5:1
--> $DIR/macro_use_imports.rs:15:5
|
LL | #[macro_use]
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use std::prelude::<macro name>`
LL | #[macro_use]
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::pub_macro`
|
= note: `-D clippy::macro-use-imports` implied by `-D warnings`

error: aborting due to previous error
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
--> $DIR/macro_use_imports.rs:15:5
|
LL | #[macro_use]
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner_mod_macro`

error: `macro_use` attributes are no longer needed in the Rust 2018 edition
--> $DIR/macro_use_imports.rs:15:5
|
LL | #[macro_use]
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::function_macro`

error: `macro_use` attributes are no longer needed in the Rust 2018 edition
--> $DIR/macro_use_imports.rs:15:5
|
LL | #[macro_use]
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::ty_macro`

error: `macro_use` attributes are no longer needed in the Rust 2018 edition
--> $DIR/macro_use_imports.rs:15:5
|
LL | #[macro_use]
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::pub_in_private_macro`

error: `macro_use` attributes are no longer needed in the Rust 2018 edition
--> $DIR/macro_use_imports.rs:17:5
|
LL | #[macro_use]
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest`

error: `macro_use` attributes are no longer needed in the Rust 2018 edition
--> $DIR/macro_use_imports.rs:19:5
|
LL | #[macro_use]
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::try_err`

error: aborting due to 7 previous errors

0 comments on commit ab2b14b

Please sign in to comment.