Skip to content

Commit 461a661

Browse files
committed
Auto merge of #8897 - tamaroning:improve_dbg, r=Alexendoo
Introduce `allow-dbg-in-tests` config value related to: Issue #8758, PR #8838 changelog: Introduced `allow-dbg-in-tests` config value. [dbg_macro] does not allow `dbg!` in test code by default.
2 parents a5ece81 + ea06a41 commit 461a661

File tree

8 files changed

+200
-6
lines changed

8 files changed

+200
-6
lines changed

clippy_lints/src/dbg_macro.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::{is_in_cfg_test, is_in_test_function};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind};
77
use rustc_lint::{LateContext, LateLintPass};
8-
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_session::{declare_tool_lint, impl_lint_pass};
99
use rustc_span::sym;
1010

1111
declare_clippy_lint! {
@@ -30,14 +30,27 @@ declare_clippy_lint! {
3030
"`dbg!` macro is intended as a debugging tool"
3131
}
3232

33-
declare_lint_pass!(DbgMacro => [DBG_MACRO]);
33+
#[derive(Copy, Clone)]
34+
pub struct DbgMacro {
35+
allow_dbg_in_tests: bool,
36+
}
37+
38+
impl_lint_pass!(DbgMacro => [DBG_MACRO]);
39+
40+
impl DbgMacro {
41+
pub fn new(allow_dbg_in_tests: bool) -> Self {
42+
DbgMacro { allow_dbg_in_tests }
43+
}
44+
}
3445

3546
impl LateLintPass<'_> for DbgMacro {
3647
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
3748
let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return };
3849
if cx.tcx.is_diagnostic_item(sym::dbg_macro, macro_call.def_id) {
39-
// we make an exception for test code
40-
if is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id) {
50+
// allows `dbg!` in test code if allow-dbg-in-test is set to true in clippy.toml
51+
if self.allow_dbg_in_tests
52+
&& (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id))
53+
{
4154
return;
4255
}
4356
let mut applicability = Applicability::MachineApplicable;

clippy_lints/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
888888
store.register_late_pass(|| Box::new(default_union_representation::DefaultUnionRepresentation));
889889
store.register_late_pass(|| Box::new(only_used_in_recursion::OnlyUsedInRecursion));
890890
store.register_late_pass(|| Box::new(significant_drop_in_scrutinee::SignificantDropInScrutinee));
891-
store.register_late_pass(|| Box::new(dbg_macro::DbgMacro));
891+
let allow_dbg_in_tests = conf.allow_dbg_in_tests;
892+
store.register_late_pass(move || Box::new(dbg_macro::DbgMacro::new(allow_dbg_in_tests)));
892893
let cargo_ignore_publish = conf.cargo_ignore_publish;
893894
store.register_late_pass(move || {
894895
Box::new(cargo::Cargo {

clippy_lints/src/utils/conf.rs

+4
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ define_Conf! {
340340
///
341341
/// Whether `unwrap` should be allowed in test functions
342342
(allow_unwrap_in_tests: bool = false),
343+
/// Lint: DBG_MACRO.
344+
///
345+
/// Whether `dbg!` should be allowed in test functions
346+
(allow_dbg_in_tests: bool = false),
343347
}
344348

345349
/// Search for the configuration file.

tests/ui-toml/dbg_macro/clippy.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow-dbg-in-tests = true

tests/ui-toml/dbg_macro/dbg_macro.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// compile-flags: --test
2+
#![warn(clippy::dbg_macro)]
3+
4+
fn foo(n: u32) -> u32 {
5+
if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
6+
}
7+
8+
fn factorial(n: u32) -> u32 {
9+
if dbg!(n <= 1) {
10+
dbg!(1)
11+
} else {
12+
dbg!(n * factorial(n - 1))
13+
}
14+
}
15+
16+
fn main() {
17+
dbg!(42);
18+
dbg!(dbg!(dbg!(42)));
19+
foo(3) + dbg!(factorial(4));
20+
dbg!(1, 2, dbg!(3, 4));
21+
dbg!(1, 2, 3, 4, 5);
22+
}
23+
24+
#[test]
25+
pub fn issue8481() {
26+
dbg!(1);
27+
}
28+
29+
#[cfg(test)]
30+
fn foo2() {
31+
dbg!(1);
32+
}
33+
34+
#[cfg(test)]
35+
mod mod1 {
36+
fn func() {
37+
dbg!(1);
38+
}
39+
}
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
error: `dbg!` macro is intended as a debugging tool
2+
--> $DIR/dbg_macro.rs:5:22
3+
|
4+
LL | if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::dbg-macro` implied by `-D warnings`
8+
help: ensure to avoid having uses of it in version control
9+
|
10+
LL | if let Some(n) = n.checked_sub(4) { n } else { n }
11+
| ~~~~~~~~~~~~~~~~
12+
13+
error: `dbg!` macro is intended as a debugging tool
14+
--> $DIR/dbg_macro.rs:9:8
15+
|
16+
LL | if dbg!(n <= 1) {
17+
| ^^^^^^^^^^^^
18+
|
19+
help: ensure to avoid having uses of it in version control
20+
|
21+
LL | if n <= 1 {
22+
| ~~~~~~
23+
24+
error: `dbg!` macro is intended as a debugging tool
25+
--> $DIR/dbg_macro.rs:10:9
26+
|
27+
LL | dbg!(1)
28+
| ^^^^^^^
29+
|
30+
help: ensure to avoid having uses of it in version control
31+
|
32+
LL | 1
33+
|
34+
35+
error: `dbg!` macro is intended as a debugging tool
36+
--> $DIR/dbg_macro.rs:12:9
37+
|
38+
LL | dbg!(n * factorial(n - 1))
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
40+
|
41+
help: ensure to avoid having uses of it in version control
42+
|
43+
LL | n * factorial(n - 1)
44+
|
45+
46+
error: `dbg!` macro is intended as a debugging tool
47+
--> $DIR/dbg_macro.rs:17:5
48+
|
49+
LL | dbg!(42);
50+
| ^^^^^^^^
51+
|
52+
help: ensure to avoid having uses of it in version control
53+
|
54+
LL | 42;
55+
| ~~
56+
57+
error: `dbg!` macro is intended as a debugging tool
58+
--> $DIR/dbg_macro.rs:18:5
59+
|
60+
LL | dbg!(dbg!(dbg!(42)));
61+
| ^^^^^^^^^^^^^^^^^^^^
62+
|
63+
help: ensure to avoid having uses of it in version control
64+
|
65+
LL | dbg!(dbg!(42));
66+
| ~~~~~~~~~~~~~~
67+
68+
error: `dbg!` macro is intended as a debugging tool
69+
--> $DIR/dbg_macro.rs:19:14
70+
|
71+
LL | foo(3) + dbg!(factorial(4));
72+
| ^^^^^^^^^^^^^^^^^^
73+
|
74+
help: ensure to avoid having uses of it in version control
75+
|
76+
LL | foo(3) + factorial(4);
77+
| ~~~~~~~~~~~~
78+
79+
error: `dbg!` macro is intended as a debugging tool
80+
--> $DIR/dbg_macro.rs:20:5
81+
|
82+
LL | dbg!(1, 2, dbg!(3, 4));
83+
| ^^^^^^^^^^^^^^^^^^^^^^
84+
|
85+
help: ensure to avoid having uses of it in version control
86+
|
87+
LL | (1, 2, dbg!(3, 4));
88+
| ~~~~~~~~~~~~~~~~~~
89+
90+
error: `dbg!` macro is intended as a debugging tool
91+
--> $DIR/dbg_macro.rs:21:5
92+
|
93+
LL | dbg!(1, 2, 3, 4, 5);
94+
| ^^^^^^^^^^^^^^^^^^^
95+
|
96+
help: ensure to avoid having uses of it in version control
97+
|
98+
LL | (1, 2, 3, 4, 5);
99+
| ~~~~~~~~~~~~~~~
100+
101+
error: aborting due to 9 previous errors
102+

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of
2+
allow-dbg-in-tests
23
allow-expect-in-tests
34
allow-unwrap-in-tests
45
allowed-scripts

tests/ui/dbg_macro.stderr

+34-1
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,38 @@ help: ensure to avoid having uses of it in version control
109109
LL | 2;
110110
| ~
111111

112-
error: aborting due to 10 previous errors
112+
error: `dbg!` macro is intended as a debugging tool
113+
--> $DIR/dbg_macro.rs:47:5
114+
|
115+
LL | dbg!(1);
116+
| ^^^^^^^
117+
|
118+
help: ensure to avoid having uses of it in version control
119+
|
120+
LL | 1;
121+
| ~
122+
123+
error: `dbg!` macro is intended as a debugging tool
124+
--> $DIR/dbg_macro.rs:52:5
125+
|
126+
LL | dbg!(1);
127+
| ^^^^^^^
128+
|
129+
help: ensure to avoid having uses of it in version control
130+
|
131+
LL | 1;
132+
| ~
133+
134+
error: `dbg!` macro is intended as a debugging tool
135+
--> $DIR/dbg_macro.rs:58:9
136+
|
137+
LL | dbg!(1);
138+
| ^^^^^^^
139+
|
140+
help: ensure to avoid having uses of it in version control
141+
|
142+
LL | 1;
143+
| ~
144+
145+
error: aborting due to 13 previous errors
113146

0 commit comments

Comments
 (0)