about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-05-19 15:46:35 +0000
committerbors <bors@rust-lang.org>2022-05-19 15:46:35 +0000
commitea960913316b5638d99a73febadfbf7ff8ca2c7d (patch)
tree621fb63060e27c4e6a24d92b5989a9a935a23a1d
parent6f8d18fe691d3197009d352bd4aa03600b8c28c5 (diff)
parentdb41df112aded9ecbbbba421189f28f6eb326554 (diff)
downloadrust-ea960913316b5638d99a73febadfbf7ff8ca2c7d.tar.gz
rust-ea960913316b5638d99a73febadfbf7ff8ca2c7d.zip
Auto merge of #8838 - tamaroning:fix_dbg, r=Jarcho,xFrednet
[dbg_macro] tolerates use of `dbg!` in items which have `#[cfg(test)]` attribute

fix: #8758
changelog: [dbg_macro] tolerates use of `dbg!` in items with `#[cfg(test)]` attribute
-rw-r--r--clippy_lints/src/dbg_macro.rs4
-rw-r--r--clippy_utils/src/lib.rs21
-rw-r--r--tests/ui/dbg_macro.rs12
3 files changed, 35 insertions, 2 deletions
diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs
index a0e5d302633..f99d793c201 100644
--- a/clippy_lints/src/dbg_macro.rs
+++ b/clippy_lints/src/dbg_macro.rs
@@ -1,7 +1,7 @@
 use clippy_utils::diagnostics::span_lint_and_sugg;
-use clippy_utils::is_in_test_function;
 use clippy_utils::macros::root_macro_call_first_node;
 use clippy_utils::source::snippet_with_applicability;
+use clippy_utils::{is_in_cfg_test, is_in_test_function};
 use rustc_errors::Applicability;
 use rustc_hir::{Expr, ExprKind};
 use rustc_lint::{LateContext, LateLintPass};
@@ -37,7 +37,7 @@ impl LateLintPass<'_> for DbgMacro {
         let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return };
         if cx.tcx.is_diagnostic_item(sym::dbg_macro, macro_call.def_id) {
             // we make an exception for test code
-            if is_in_test_function(cx.tcx, expr.hir_id) {
+            if is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id) {
                 return;
             }
             let mut applicability = Applicability::MachineApplicable;
diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs
index 25596168d11..8f9e687fb6a 100644
--- a/clippy_utils/src/lib.rs
+++ b/clippy_utils/src/lib.rs
@@ -2146,6 +2146,27 @@ pub fn is_in_test_function(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
     })
 }
 
+/// Checks if the item containing the given `HirId` has `#[cfg(test)]` attribute applied
+///
+/// Note: Add `// compile-flags: --test` to UI tests with a `#[cfg(test)]` function
+pub fn is_in_cfg_test(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
+    fn is_cfg_test(attr: &Attribute) -> bool {
+        if attr.has_name(sym::cfg)
+            && let Some(items) = attr.meta_item_list()
+            && let [item] = &*items
+            && item.has_name(sym::test)
+        {
+            true
+        } else {
+            false
+        }
+    }
+    tcx.hir()
+        .parent_iter(id)
+        .flat_map(|(parent_id, _)| tcx.hir().attrs(parent_id))
+        .any(is_cfg_test)
+}
+
 /// Checks whether item either has `test` attribute applied, or
 /// is a module with `test` in its name.
 ///
diff --git a/tests/ui/dbg_macro.rs b/tests/ui/dbg_macro.rs
index baf01174b67..25294e8c766 100644
--- a/tests/ui/dbg_macro.rs
+++ b/tests/ui/dbg_macro.rs
@@ -46,3 +46,15 @@ mod issue7274 {
 pub fn issue8481() {
     dbg!(1);
 }
+
+#[cfg(test)]
+fn foo2() {
+    dbg!(1);
+}
+
+#[cfg(test)]
+mod mod1 {
+    fn func() {
+        dbg!(1);
+    }
+}