about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSerial <69764315+Serial-ATA@users.noreply.github.com>2022-04-22 18:03:23 -0400
committerSerial <69764315+Serial-ATA@users.noreply.github.com>2022-04-22 18:09:14 -0400
commitf20e890a4b53848771f2041312cd52d31fb015bc (patch)
treee8ec98020a07e20d99d22ae5a1cd675cfc02c729
parentcef882cc9dfae7980a1712609f8d00bfaf78062c (diff)
downloadrust-f20e890a4b53848771f2041312cd52d31fb015bc.tar.gz
rust-f20e890a4b53848771f2041312cd52d31fb015bc.zip
Add macro export exemption to `redundant_pub_crate`
-rw-r--r--clippy_lints/src/redundant_pub_crate.rs19
-rw-r--r--tests/ui/redundant_pub_crate.fixed10
-rw-r--r--tests/ui/redundant_pub_crate.rs10
3 files changed, 37 insertions, 2 deletions
diff --git a/clippy_lints/src/redundant_pub_crate.rs b/clippy_lints/src/redundant_pub_crate.rs
index 2cee3c14d7f..3e6086d56f0 100644
--- a/clippy_lints/src/redundant_pub_crate.rs
+++ b/clippy_lints/src/redundant_pub_crate.rs
@@ -1,8 +1,10 @@
 use clippy_utils::diagnostics::span_lint_and_then;
 use rustc_errors::Applicability;
+use rustc_hir::def::{DefKind, Res};
 use rustc_hir::{Item, ItemKind, VisibilityKind};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::{declare_tool_lint, impl_lint_pass};
+use rustc_span::hygiene::MacroKind;
 
 declare_clippy_lint! {
     /// ### What it does
@@ -41,8 +43,11 @@ impl_lint_pass!(RedundantPubCrate => [REDUNDANT_PUB_CRATE]);
 
 impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
-        if let VisibilityKind::Crate { .. } = item.vis.node {
-            if !cx.access_levels.is_exported(item.def_id) && self.is_exported.last() == Some(&false) {
+        if_chain! {
+            if let VisibilityKind::Crate { .. } = item.vis.node;
+            if !cx.access_levels.is_exported(item.def_id) && self.is_exported.last() == Some(&false);
+            if is_not_macro_export(item);
+            then {
                 let span = item.span.with_hi(item.ident.span.hi());
                 let descr = cx.tcx.def_kind(item.def_id).descr(item.def_id.to_def_id());
                 span_lint_and_then(
@@ -73,3 +78,13 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
         }
     }
 }
+
+fn is_not_macro_export<'tcx>(item: &'tcx Item<'tcx>) -> bool {
+    if let ItemKind::Use(path, _) = item.kind {
+        if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = path.res {
+            return false;
+        }
+    }
+
+    true
+}
diff --git a/tests/ui/redundant_pub_crate.fixed b/tests/ui/redundant_pub_crate.fixed
index 25f2fd061b8..106947de68c 100644
--- a/tests/ui/redundant_pub_crate.fixed
+++ b/tests/ui/redundant_pub_crate.fixed
@@ -104,4 +104,14 @@ mod m4 {
 
 pub use m4::*;
 
+mod issue_8732 {
+    #[allow(unused_macros)]
+    macro_rules! some_macro {
+        () => {};
+    }
+
+    #[allow(unused_imports)]
+    pub(crate) use some_macro; // ok: macro exports are exempt
+}
+
 fn main() {}
diff --git a/tests/ui/redundant_pub_crate.rs b/tests/ui/redundant_pub_crate.rs
index 616286b4f39..f96cfd31843 100644
--- a/tests/ui/redundant_pub_crate.rs
+++ b/tests/ui/redundant_pub_crate.rs
@@ -104,4 +104,14 @@ mod m4 {
 
 pub use m4::*;
 
+mod issue_8732 {
+    #[allow(unused_macros)]
+    macro_rules! some_macro {
+        () => {};
+    }
+
+    #[allow(unused_imports)]
+    pub(crate) use some_macro; // ok: macro exports are exempt
+}
+
 fn main() {}