Skip to content

Commit 6ef84d0

Browse files
committed
lint nested dbg! macros, split tests
1 parent 747200a commit 6ef84d0

File tree

9 files changed

+269
-103
lines changed

9 files changed

+269
-103
lines changed

clippy_lints/src/dbg_macro.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::root_macro_call;
33
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::{is_in_cfg_test, is_in_test_function};
5+
use rustc_data_structures::fx::FxHashSet;
56
use rustc_errors::Applicability;
67
use rustc_hir::{Expr, ExprKind, Node};
78
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -31,12 +32,12 @@ declare_clippy_lint! {
3132
"`dbg!` macro is intended as a debugging tool"
3233
}
3334

34-
#[derive(Copy, Clone)]
35+
#[derive(Clone)]
3536
pub struct DbgMacro {
3637
allow_dbg_in_tests: bool,
37-
/// Keep tracking of the previous `dbg!` macro call site in order to
38-
/// skip any other expressions from the same expansion, including nested macro calls.
39-
prev_dbg_call_site: Span,
38+
/// Keep tracks the `dbg!` macro callsites that are already checked,
39+
/// so that we can save some performance by skipping any expressions from the same expansion.
40+
checked_dbg_call_site: FxHashSet<Span>,
4041
}
4142

4243
impl_lint_pass!(DbgMacro => [DBG_MACRO]);
@@ -45,7 +46,7 @@ impl DbgMacro {
4546
pub fn new(allow_dbg_in_tests: bool) -> Self {
4647
DbgMacro {
4748
allow_dbg_in_tests,
48-
prev_dbg_call_site: Span::default(),
49+
checked_dbg_call_site: FxHashSet::default(),
4950
}
5051
}
5152
}
@@ -57,11 +58,10 @@ impl LateLintPass<'_> for DbgMacro {
5758
else {
5859
return;
5960
};
60-
// skip previous checked exprs
61-
if self.prev_dbg_call_site.contains(macro_call.span) {
61+
if self.checked_dbg_call_site.contains(&macro_call.span) {
6262
return;
6363
}
64-
self.prev_dbg_call_site = macro_call.span;
64+
self.checked_dbg_call_site.insert(macro_call.span);
6565

6666
// allows `dbg!` in test code if allow-dbg-in-test is set to true in clippy.toml
6767
if self.allow_dbg_in_tests && (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id))
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@compile-flags: --test
2+
#![warn(clippy::dbg_macro)]
3+
#![allow(clippy::unnecessary_operation, clippy::no_effect)]
4+
5+
fn foo(n: u32) -> u32 {
6+
if let Some(n) = n.checked_sub(4) { n } else { n }
7+
}
8+
9+
fn factorial(n: u32) -> u32 {
10+
if n <= 1 {
11+
1
12+
} else {
13+
n * factorial(n - 1)
14+
}
15+
}
16+
17+
fn main() {
18+
42;
19+
foo(3) + factorial(4);
20+
(1, 2, 3, 4, 5);
21+
}
22+
23+
#[test]
24+
pub fn issue8481() {
25+
dbg!(1);
26+
}
27+
28+
#[cfg(test)]
29+
fn foo2() {
30+
dbg!(1);
31+
}
32+
33+
#[cfg(test)]
34+
mod mod1 {
35+
fn func() {
36+
dbg!(1);
37+
}
38+
}

tests/ui-toml/dbg_macro/dbg_macro.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@compile-flags: --test
22
#![warn(clippy::dbg_macro)]
3-
//@no-rustfix
3+
#![allow(clippy::unnecessary_operation, clippy::no_effect)]
4+
45
fn foo(n: u32) -> u32 {
56
if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
67
}
@@ -15,9 +16,7 @@ fn factorial(n: u32) -> u32 {
1516

1617
fn main() {
1718
dbg!(42);
18-
dbg!(dbg!(dbg!(42)));
1919
foo(3) + dbg!(factorial(4));
20-
dbg!(1, 2, dbg!(3, 4));
2120
dbg!(1, 2, 3, 4, 5);
2221
}
2322

+6-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: the `dbg!` macro is intended as a debugging tool
2-
--> tests/ui-toml/dbg_macro/dbg_macro.rs:5:22
2+
--> tests/ui-toml/dbg_macro/dbg_macro.rs:6:22
33
|
44
LL | if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
55
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -12,7 +12,7 @@ LL | if let Some(n) = n.checked_sub(4) { n } else { n }
1212
| ~~~~~~~~~~~~~~~~
1313

1414
error: the `dbg!` macro is intended as a debugging tool
15-
--> tests/ui-toml/dbg_macro/dbg_macro.rs:9:8
15+
--> tests/ui-toml/dbg_macro/dbg_macro.rs:10:8
1616
|
1717
LL | if dbg!(n <= 1) {
1818
| ^^^^^^^^^^^^
@@ -23,7 +23,7 @@ LL | if n <= 1 {
2323
| ~~~~~~
2424

2525
error: the `dbg!` macro is intended as a debugging tool
26-
--> tests/ui-toml/dbg_macro/dbg_macro.rs:10:9
26+
--> tests/ui-toml/dbg_macro/dbg_macro.rs:11:9
2727
|
2828
LL | dbg!(1)
2929
| ^^^^^^^
@@ -34,7 +34,7 @@ LL | 1
3434
|
3535

3636
error: the `dbg!` macro is intended as a debugging tool
37-
--> tests/ui-toml/dbg_macro/dbg_macro.rs:12:9
37+
--> tests/ui-toml/dbg_macro/dbg_macro.rs:13:9
3838
|
3939
LL | dbg!(n * factorial(n - 1))
4040
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -45,7 +45,7 @@ LL | n * factorial(n - 1)
4545
|
4646

4747
error: the `dbg!` macro is intended as a debugging tool
48-
--> tests/ui-toml/dbg_macro/dbg_macro.rs:17:5
48+
--> tests/ui-toml/dbg_macro/dbg_macro.rs:18:5
4949
|
5050
LL | dbg!(42);
5151
| ^^^^^^^^
@@ -55,17 +55,6 @@ help: remove the invocation before committing it to a version control system
5555
LL | 42;
5656
| ~~
5757

58-
error: the `dbg!` macro is intended as a debugging tool
59-
--> tests/ui-toml/dbg_macro/dbg_macro.rs:18:5
60-
|
61-
LL | dbg!(dbg!(dbg!(42)));
62-
| ^^^^^^^^^^^^^^^^^^^^
63-
|
64-
help: remove the invocation before committing it to a version control system
65-
|
66-
LL | dbg!(dbg!(42));
67-
| ~~~~~~~~~~~~~~
68-
6958
error: the `dbg!` macro is intended as a debugging tool
7059
--> tests/ui-toml/dbg_macro/dbg_macro.rs:19:14
7160
|
@@ -80,17 +69,6 @@ LL | foo(3) + factorial(4);
8069
error: the `dbg!` macro is intended as a debugging tool
8170
--> tests/ui-toml/dbg_macro/dbg_macro.rs:20:5
8271
|
83-
LL | dbg!(1, 2, dbg!(3, 4));
84-
| ^^^^^^^^^^^^^^^^^^^^^^
85-
|
86-
help: remove the invocation before committing it to a version control system
87-
|
88-
LL | (1, 2, dbg!(3, 4));
89-
| ~~~~~~~~~~~~~~~~~~
90-
91-
error: the `dbg!` macro is intended as a debugging tool
92-
--> tests/ui-toml/dbg_macro/dbg_macro.rs:21:5
93-
|
9472
LL | dbg!(1, 2, 3, 4, 5);
9573
| ^^^^^^^^^^^^^^^^^^^
9674
|
@@ -99,5 +77,5 @@ help: remove the invocation before committing it to a version control system
9977
LL | (1, 2, 3, 4, 5);
10078
| ~~~~~~~~~~~~~~~
10179

102-
error: aborting due to 9 previous errors
80+
error: aborting due to 7 previous errors
10381

tests/ui/dbg_macro/dbg_macro.fixed

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#![warn(clippy::dbg_macro)]
2+
#![allow(clippy::unnecessary_operation, clippy::no_effect)]
3+
4+
fn foo(n: u32) -> u32 {
5+
if let Some(n) = n.checked_sub(4) { n } else { n }
6+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
7+
}
8+
fn bar(_: ()) {}
9+
10+
fn factorial(n: u32) -> u32 {
11+
if n <= 1 {
12+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
13+
1
14+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
15+
} else {
16+
n * factorial(n - 1)
17+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
18+
}
19+
}
20+
21+
fn main() {
22+
42;
23+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
24+
foo(3) + factorial(4);
25+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
26+
(1, 2, 3, 4, 5);
27+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
28+
}
29+
30+
fn issue9914() {
31+
macro_rules! foo {
32+
($x:expr) => {
33+
$x;
34+
};
35+
}
36+
macro_rules! foo2 {
37+
($x:expr) => {
38+
$x;
39+
};
40+
}
41+
macro_rules! expand_to_dbg {
42+
() => {
43+
dbg!();
44+
};
45+
}
46+
47+
48+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
49+
#[allow(clippy::let_unit_value)]
50+
let _ = ();
51+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
52+
bar(());
53+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
54+
foo!(());
55+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
56+
foo2!(foo!(()));
57+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
58+
expand_to_dbg!();
59+
}
60+
61+
mod issue7274 {
62+
trait Thing<'b> {
63+
fn foo(&self);
64+
}
65+
66+
macro_rules! define_thing {
67+
($thing:ident, $body:expr) => {
68+
impl<'a> Thing<'a> for $thing {
69+
fn foo<'b>(&self) {
70+
$body
71+
}
72+
}
73+
};
74+
}
75+
76+
struct MyThing;
77+
define_thing!(MyThing, {
78+
2;
79+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
80+
});
81+
}
82+
83+
#[test]
84+
pub fn issue8481() {
85+
1;
86+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
87+
}
88+
89+
#[cfg(test)]
90+
fn foo2() {
91+
1;
92+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
93+
}
94+
95+
#[cfg(test)]
96+
mod mod1 {
97+
fn func() {
98+
1;
99+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
100+
}
101+
}
102+
103+
mod issue12131 {
104+
fn dbg_in_print(s: &str) {
105+
println!("dbg: {:?}", s);
106+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
107+
print!("{}", s);
108+
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
109+
}
110+
}

tests/ui/dbg_macro/dbg_macro.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
//@no-rustfix
2-
31
#![warn(clippy::dbg_macro)]
4-
5-
#[path = "auxiliary/submodule.rs"]
6-
mod submodule;
2+
#![allow(clippy::unnecessary_operation, clippy::no_effect)]
73

84
fn foo(n: u32) -> u32 {
95
if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
@@ -25,12 +21,8 @@ fn factorial(n: u32) -> u32 {
2521
fn main() {
2622
dbg!(42);
2723
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
28-
dbg!(dbg!(dbg!(42)));
29-
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
3024
foo(3) + dbg!(factorial(4));
3125
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
32-
dbg!(1, 2, dbg!(3, 4));
33-
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
3426
dbg!(1, 2, 3, 4, 5);
3527
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
3628
}

0 commit comments

Comments
 (0)