Skip to content

Commit

Permalink
Auto merge of rust-lang#8500 - rust-lang:testless-dbg-macro, r=Manish…
Browse files Browse the repository at this point in the history
…earth

Omit dbg_macro in test code

This fixes rust-lang#8481.

---

changelog: none
  • Loading branch information
bors committed Mar 3, 2022
2 parents c5faf99 + 6390723 commit 53189ad
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
5 changes: 5 additions & 0 deletions clippy_lints/src/dbg_macro.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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 rustc_errors::Applicability;
Expand Down Expand Up @@ -35,6 +36,10 @@ impl LateLintPass<'_> for DbgMacro {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
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) {
return;
}
let mut applicability = Applicability::MachineApplicable;
let suggestion = match expr.peel_drop_temps().kind {
// dbg!()
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/dbg_macro.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// compile-flags: --test
#![warn(clippy::dbg_macro)]

fn foo(n: u32) -> u32 {
Expand Down Expand Up @@ -40,3 +41,8 @@ mod issue7274 {
dbg!(2);
});
}

#[test]
pub fn issue8481() {
dbg!(1);
}
20 changes: 10 additions & 10 deletions tests/ui/dbg_macro.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:4:22
--> $DIR/dbg_macro.rs:5:22
|
LL | if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -11,7 +11,7 @@ LL | if let Some(n) = n.checked_sub(4) { n } else { n }
| ~~~~~~~~~~~~~~~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:8:8
--> $DIR/dbg_macro.rs:9:8
|
LL | if dbg!(n <= 1) {
| ^^^^^^^^^^^^
Expand All @@ -22,7 +22,7 @@ LL | if n <= 1 {
| ~~~~~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:9:9
--> $DIR/dbg_macro.rs:10:9
|
LL | dbg!(1)
| ^^^^^^^
Expand All @@ -33,7 +33,7 @@ LL | 1
|

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:11:9
--> $DIR/dbg_macro.rs:12:9
|
LL | dbg!(n * factorial(n - 1))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -44,7 +44,7 @@ LL | n * factorial(n - 1)
|

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:16:5
--> $DIR/dbg_macro.rs:17:5
|
LL | dbg!(42);
| ^^^^^^^^
Expand All @@ -55,7 +55,7 @@ LL | 42;
| ~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:17:5
--> $DIR/dbg_macro.rs:18:5
|
LL | dbg!(dbg!(dbg!(42)));
| ^^^^^^^^^^^^^^^^^^^^
Expand All @@ -66,7 +66,7 @@ LL | dbg!(dbg!(42));
| ~~~~~~~~~~~~~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:18:14
--> $DIR/dbg_macro.rs:19:14
|
LL | foo(3) + dbg!(factorial(4));
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -77,7 +77,7 @@ LL | foo(3) + factorial(4);
| ~~~~~~~~~~~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:19:5
--> $DIR/dbg_macro.rs:20:5
|
LL | dbg!(1, 2, dbg!(3, 4));
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -88,7 +88,7 @@ LL | (1, 2, dbg!(3, 4));
| ~~~~~~~~~~~~~~~~~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:20:5
--> $DIR/dbg_macro.rs:21:5
|
LL | dbg!(1, 2, 3, 4, 5);
| ^^^^^^^^^^^^^^^^^^^
Expand All @@ -99,7 +99,7 @@ LL | (1, 2, 3, 4, 5);
| ~~~~~~~~~~~~~~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:40:9
--> $DIR/dbg_macro.rs:41:9
|
LL | dbg!(2);
| ^^^^^^^
Expand Down

0 comments on commit 53189ad

Please sign in to comment.