Skip to content

Introduce allow-dbg-in-tests config value #8897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions clippy_lints/src/dbg_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::sym;

declare_clippy_lint! {
Expand All @@ -30,14 +30,27 @@ declare_clippy_lint! {
"`dbg!` macro is intended as a debugging tool"
}

declare_lint_pass!(DbgMacro => [DBG_MACRO]);
#[derive(Copy, Clone)]
pub struct DbgMacro {
allow_dbg_in_tests: bool,
}

impl_lint_pass!(DbgMacro => [DBG_MACRO]);

impl DbgMacro {
pub fn new(allow_dbg_in_tests: bool) -> Self {
DbgMacro { allow_dbg_in_tests }
}
}

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) || is_in_cfg_test(cx.tcx, expr.hir_id) {
// allows `dbg!` in test code if allow-dbg-in-test is set to true in clippy.toml
if self.allow_dbg_in_tests
&& (is_in_test_function(cx.tcx, expr.hir_id) || is_in_cfg_test(cx.tcx, expr.hir_id))
{
return;
}
let mut applicability = Applicability::MachineApplicable;
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| Box::new(default_union_representation::DefaultUnionRepresentation));
store.register_late_pass(|| Box::new(only_used_in_recursion::OnlyUsedInRecursion));
store.register_late_pass(|| Box::new(significant_drop_in_scrutinee::SignificantDropInScrutinee));
store.register_late_pass(|| Box::new(dbg_macro::DbgMacro));
let allow_dbg_in_tests = conf.allow_dbg_in_tests;
store.register_late_pass(move || Box::new(dbg_macro::DbgMacro::new(allow_dbg_in_tests)));
let cargo_ignore_publish = conf.cargo_ignore_publish;
store.register_late_pass(move || {
Box::new(cargo::Cargo {
Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/utils/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ define_Conf! {
///
/// Whether `unwrap` should be allowed in test functions
(allow_unwrap_in_tests: bool = false),
/// Lint: DBG_MACRO.
///
/// Whether `dbg!` should be allowed in test functions
(allow_dbg_in_tests: bool = false),
}

/// Search for the configuration file.
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/dbg_macro/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allow-dbg-in-tests = true
39 changes: 39 additions & 0 deletions tests/ui-toml/dbg_macro/dbg_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// compile-flags: --test
#![warn(clippy::dbg_macro)]

fn foo(n: u32) -> u32 {
if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
}

fn factorial(n: u32) -> u32 {
if dbg!(n <= 1) {
dbg!(1)
} else {
dbg!(n * factorial(n - 1))
}
}

fn main() {
dbg!(42);
dbg!(dbg!(dbg!(42)));
foo(3) + dbg!(factorial(4));
dbg!(1, 2, dbg!(3, 4));
dbg!(1, 2, 3, 4, 5);
}

#[test]
pub fn issue8481() {
dbg!(1);
}

#[cfg(test)]
fn foo2() {
dbg!(1);
}

#[cfg(test)]
mod mod1 {
fn func() {
dbg!(1);
}
}
102 changes: 102 additions & 0 deletions tests/ui-toml/dbg_macro/dbg_macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:5:22
|
LL | if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::dbg-macro` implied by `-D warnings`
help: ensure to avoid having uses of it in version control
|
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:9:8
|
LL | if dbg!(n <= 1) {
| ^^^^^^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | if n <= 1 {
| ~~~~~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:10:9
|
LL | dbg!(1)
| ^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | 1
|

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:12:9
|
LL | dbg!(n * factorial(n - 1))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | n * factorial(n - 1)
|

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:17:5
|
LL | dbg!(42);
| ^^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | 42;
| ~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:18:5
|
LL | dbg!(dbg!(dbg!(42)));
| ^^^^^^^^^^^^^^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | dbg!(dbg!(42));
| ~~~~~~~~~~~~~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:19:14
|
LL | foo(3) + dbg!(factorial(4));
| ^^^^^^^^^^^^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | foo(3) + factorial(4);
| ~~~~~~~~~~~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:20:5
|
LL | dbg!(1, 2, dbg!(3, 4));
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | (1, 2, dbg!(3, 4));
| ~~~~~~~~~~~~~~~~~~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:21:5
|
LL | dbg!(1, 2, 3, 4, 5);
| ^^^^^^^^^^^^^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | (1, 2, 3, 4, 5);
| ~~~~~~~~~~~~~~~

error: aborting due to 9 previous errors

1 change: 1 addition & 0 deletions tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of
allow-dbg-in-tests
allow-expect-in-tests
allow-unwrap-in-tests
allowed-scripts
Expand Down
35 changes: 34 additions & 1 deletion tests/ui/dbg_macro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,38 @@ help: ensure to avoid having uses of it in version control
LL | 2;
| ~

error: aborting due to 10 previous errors
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:47:5
|
LL | dbg!(1);
| ^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | 1;
| ~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:52:5
|
LL | dbg!(1);
| ^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | 1;
| ~

error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:58:9
|
LL | dbg!(1);
| ^^^^^^^
|
help: ensure to avoid having uses of it in version control
|
LL | 1;
| ~

error: aborting due to 13 previous errors