Skip to content

Commit

Permalink
lint nested dbg! macros, split tests
Browse files Browse the repository at this point in the history
  • Loading branch information
J-ZhengLi committed Feb 20, 2024
1 parent 747200a commit 6ef84d0
Show file tree
Hide file tree
Showing 9 changed files with 269 additions and 103 deletions.
16 changes: 8 additions & 8 deletions clippy_lints/src/dbg_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::macros::root_macro_call;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::{is_in_cfg_test, is_in_test_function};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, Node};
use rustc_lint::{LateContext, LateLintPass, LintContext};
Expand Down Expand Up @@ -31,12 +32,12 @@ declare_clippy_lint! {
"`dbg!` macro is intended as a debugging tool"
}

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

impl_lint_pass!(DbgMacro => [DBG_MACRO]);
Expand All @@ -45,7 +46,7 @@ impl DbgMacro {
pub fn new(allow_dbg_in_tests: bool) -> Self {
DbgMacro {
allow_dbg_in_tests,
prev_dbg_call_site: Span::default(),
checked_dbg_call_site: FxHashSet::default(),
}
}
}
Expand All @@ -57,11 +58,10 @@ impl LateLintPass<'_> for DbgMacro {
else {
return;
};
// skip previous checked exprs
if self.prev_dbg_call_site.contains(macro_call.span) {
if self.checked_dbg_call_site.contains(&macro_call.span) {
return;
}
self.prev_dbg_call_site = macro_call.span;
self.checked_dbg_call_site.insert(macro_call.span);

// 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))
Expand Down
38 changes: 38 additions & 0 deletions tests/ui-toml/dbg_macro/dbg_macro.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//@compile-flags: --test
#![warn(clippy::dbg_macro)]
#![allow(clippy::unnecessary_operation, clippy::no_effect)]

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

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

fn main() {
42;
foo(3) + factorial(4);
(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);
}
}
5 changes: 2 additions & 3 deletions tests/ui-toml/dbg_macro/dbg_macro.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@compile-flags: --test
#![warn(clippy::dbg_macro)]
//@no-rustfix
#![allow(clippy::unnecessary_operation, clippy::no_effect)]

fn foo(n: u32) -> u32 {
if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
}
Expand All @@ -15,9 +16,7 @@ fn factorial(n: u32) -> u32 {

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);
}

Expand Down
34 changes: 6 additions & 28 deletions tests/ui-toml/dbg_macro/dbg_macro.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui-toml/dbg_macro/dbg_macro.rs:5:22
--> tests/ui-toml/dbg_macro/dbg_macro.rs:6:22
|
LL | if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -12,7 +12,7 @@ LL | if let Some(n) = n.checked_sub(4) { n } else { n }
| ~~~~~~~~~~~~~~~~

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

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

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

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

error: the `dbg!` macro is intended as a debugging tool
--> tests/ui-toml/dbg_macro/dbg_macro.rs:18:5
|
LL | dbg!(dbg!(dbg!(42)));
| ^^^^^^^^^^^^^^^^^^^^
|
help: remove the invocation before committing it to a version control system
|
LL | dbg!(dbg!(42));
| ~~~~~~~~~~~~~~

error: the `dbg!` macro is intended as a debugging tool
--> tests/ui-toml/dbg_macro/dbg_macro.rs:19:14
|
Expand All @@ -80,17 +69,6 @@ LL | foo(3) + factorial(4);
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui-toml/dbg_macro/dbg_macro.rs:20:5
|
LL | dbg!(1, 2, dbg!(3, 4));
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the invocation before committing it to a version control system
|
LL | (1, 2, dbg!(3, 4));
| ~~~~~~~~~~~~~~~~~~

error: the `dbg!` macro is intended as a debugging tool
--> tests/ui-toml/dbg_macro/dbg_macro.rs:21:5
|
LL | dbg!(1, 2, 3, 4, 5);
| ^^^^^^^^^^^^^^^^^^^
|
Expand All @@ -99,5 +77,5 @@ help: remove the invocation before committing it to a version control system
LL | (1, 2, 3, 4, 5);
| ~~~~~~~~~~~~~~~

error: aborting due to 9 previous errors
error: aborting due to 7 previous errors

110 changes: 110 additions & 0 deletions tests/ui/dbg_macro/dbg_macro.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#![warn(clippy::dbg_macro)]
#![allow(clippy::unnecessary_operation, clippy::no_effect)]

fn foo(n: u32) -> u32 {
if let Some(n) = n.checked_sub(4) { n } else { n }
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
fn bar(_: ()) {}

fn factorial(n: u32) -> u32 {
if n <= 1 {
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
1
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
} else {
n * factorial(n - 1)
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
}

fn main() {
42;
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
foo(3) + factorial(4);
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
(1, 2, 3, 4, 5);
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}

fn issue9914() {
macro_rules! foo {
($x:expr) => {
$x;
};
}
macro_rules! foo2 {
($x:expr) => {
$x;
};
}
macro_rules! expand_to_dbg {
() => {
dbg!();
};
}


//~^ ERROR: the `dbg!` macro is intended as a debugging tool
#[allow(clippy::let_unit_value)]
let _ = ();
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
bar(());
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
foo!(());
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
foo2!(foo!(()));
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
expand_to_dbg!();
}

mod issue7274 {
trait Thing<'b> {
fn foo(&self);
}

macro_rules! define_thing {
($thing:ident, $body:expr) => {
impl<'a> Thing<'a> for $thing {
fn foo<'b>(&self) {
$body
}
}
};
}

struct MyThing;
define_thing!(MyThing, {
2;
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
});
}

#[test]
pub fn issue8481() {
1;
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}

#[cfg(test)]
fn foo2() {
1;
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}

#[cfg(test)]
mod mod1 {
fn func() {
1;
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
}

mod issue12131 {
fn dbg_in_print(s: &str) {
println!("dbg: {:?}", s);
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
print!("{}", s);
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
}
10 changes: 1 addition & 9 deletions tests/ui/dbg_macro/dbg_macro.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//@no-rustfix

#![warn(clippy::dbg_macro)]

#[path = "auxiliary/submodule.rs"]
mod submodule;
#![allow(clippy::unnecessary_operation, clippy::no_effect)]

fn foo(n: u32) -> u32 {
if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
Expand All @@ -25,12 +21,8 @@ fn factorial(n: u32) -> u32 {
fn main() {
dbg!(42);
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
dbg!(dbg!(dbg!(42)));
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
foo(3) + dbg!(factorial(4));
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
dbg!(1, 2, dbg!(3, 4));
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
dbg!(1, 2, 3, 4, 5);
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
}
Expand Down
Loading

0 comments on commit 6ef84d0

Please sign in to comment.