lint nested dbg! macros, split tests

This commit is contained in:
J-ZhengLi 2024-02-21 02:11:19 +08:00
parent 1fe884401a
commit 0535f55831
9 changed files with 269 additions and 103 deletions

View File

@ -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};
@ -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]);
@ -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(),
}
}
}
@ -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))

View File

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

View File

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

View File

@ -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 }
| ^^^^^^^^^^^^^^^^^^^^^^
@ -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) {
| ^^^^^^^^^^^^
@ -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)
| ^^^^^^^
@ -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))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -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);
| ^^^^^^^^
@ -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
|
@ -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);
| ^^^^^^^^^^^^^^^^^^^
|
@ -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

View File

@ -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
}
}

View File

@ -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 }
@ -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
}

View File

@ -1,30 +1,18 @@
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/auxiliary/submodule.rs:2:5
|
LL | dbg!();
| ^^^^^^^
|
= note: `-D clippy::dbg-macro` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::dbg_macro)]`
help: remove the invocation before committing it to a version control system
|
LL - dbg!();
LL +
|
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:9:22
--> tests/ui/dbg_macro/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: to override `-D warnings` add `#[allow(clippy::dbg_macro)]`
help: remove the invocation before committing it to a version control system
|
LL | if let Some(n) = n.checked_sub(4) { n } else { n }
| ~~~~~~~~~~~~~~~~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:15:8
--> tests/ui/dbg_macro/dbg_macro.rs:11:8
|
LL | if dbg!(n <= 1) {
| ^^^^^^^^^^^^
@ -35,7 +23,7 @@ LL | if n <= 1 {
| ~~~~~~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:17:9
--> tests/ui/dbg_macro/dbg_macro.rs:13:9
|
LL | dbg!(1)
| ^^^^^^^
@ -46,7 +34,7 @@ LL | 1
|
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:20:9
--> tests/ui/dbg_macro/dbg_macro.rs:16:9
|
LL | dbg!(n * factorial(n - 1))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -57,7 +45,7 @@ LL | n * factorial(n - 1)
|
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:26:5
--> tests/ui/dbg_macro/dbg_macro.rs:22:5
|
LL | dbg!(42);
| ^^^^^^^^
@ -68,18 +56,7 @@ LL | 42;
| ~~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:28: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/dbg_macro/dbg_macro.rs:30:14
--> tests/ui/dbg_macro/dbg_macro.rs:24:14
|
LL | foo(3) + dbg!(factorial(4));
| ^^^^^^^^^^^^^^^^^^
@ -90,18 +67,7 @@ LL | foo(3) + factorial(4);
| ~~~~~~~~~~~~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:32: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/dbg_macro/dbg_macro.rs:34:5
--> tests/ui/dbg_macro/dbg_macro.rs:26:5
|
LL | dbg!(1, 2, 3, 4, 5);
| ^^^^^^^^^^^^^^^^^^^
@ -112,7 +78,7 @@ LL | (1, 2, 3, 4, 5);
| ~~~~~~~~~~~~~~~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:55:5
--> tests/ui/dbg_macro/dbg_macro.rs:47:5
|
LL | dbg!();
| ^^^^^^^
@ -124,7 +90,7 @@ LL +
|
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:58:13
--> tests/ui/dbg_macro/dbg_macro.rs:50:13
|
LL | let _ = dbg!();
| ^^^^^^
@ -135,7 +101,7 @@ LL | let _ = ();
| ~~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:60:9
--> tests/ui/dbg_macro/dbg_macro.rs:52:9
|
LL | bar(dbg!());
| ^^^^^^
@ -146,7 +112,7 @@ LL | bar(());
| ~~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:62:10
--> tests/ui/dbg_macro/dbg_macro.rs:54:10
|
LL | foo!(dbg!());
| ^^^^^^
@ -157,7 +123,7 @@ LL | foo!(());
| ~~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:64:16
--> tests/ui/dbg_macro/dbg_macro.rs:56:16
|
LL | foo2!(foo!(dbg!()));
| ^^^^^^
@ -168,7 +134,7 @@ LL | foo2!(foo!(()));
| ~~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:86:9
--> tests/ui/dbg_macro/dbg_macro.rs:78:9
|
LL | dbg!(2);
| ^^^^^^^
@ -179,7 +145,7 @@ LL | 2;
| ~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:93:5
--> tests/ui/dbg_macro/dbg_macro.rs:85:5
|
LL | dbg!(1);
| ^^^^^^^
@ -190,7 +156,7 @@ LL | 1;
| ~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:99:5
--> tests/ui/dbg_macro/dbg_macro.rs:91:5
|
LL | dbg!(1);
| ^^^^^^^
@ -201,7 +167,7 @@ LL | 1;
| ~
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro.rs:106:9
--> tests/ui/dbg_macro/dbg_macro.rs:98:9
|
LL | dbg!(1);
| ^^^^^^^
@ -212,7 +178,7 @@ LL | 1;
| ~
error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:113:31
--> tests/ui/dbg_macro/dbg_macro.rs:105:31
|
LL | println!("dbg: {:?}", dbg!(s));
| ^^^^^^^
@ -223,7 +189,7 @@ LL | println!("dbg: {:?}", s);
| ~
error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:115:22
--> tests/ui/dbg_macro/dbg_macro.rs:107:22
|
LL | print!("{}", dbg!(s));
| ^^^^^^^
@ -233,5 +199,5 @@ help: remove the invocation before committing it to a version control system
LL | print!("{}", s);
| ~
error: aborting due to 21 previous errors
error: aborting due to 18 previous errors

View File

@ -0,0 +1,12 @@
//@no-rustfix
#![warn(clippy::dbg_macro)]
#[path = "auxiliary/submodule.rs"]
mod submodule;
fn main() {
dbg!(dbg!(dbg!(42)));
//~^ 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
}

View File

@ -0,0 +1,71 @@
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/auxiliary/submodule.rs:2:5
|
LL | dbg!();
| ^^^^^^^
|
= note: `-D clippy::dbg-macro` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::dbg_macro)]`
help: remove the invocation before committing it to a version control system
|
LL - dbg!();
LL +
|
error: the `dbg!` macro is intended as a debugging tool
--> tests/ui/dbg_macro/dbg_macro_unfixable.rs:8: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/dbg_macro/dbg_macro_unfixable.rs:8:10
|
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/dbg_macro/dbg_macro_unfixable.rs:8:15
|
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/dbg_macro/dbg_macro_unfixable.rs:10: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/dbg_macro/dbg_macro_unfixable.rs:10:16
|
LL | dbg!(1, 2, dbg!(3, 4));
| ^^^^^^^^^^
|
help: remove the invocation before committing it to a version control system
|
LL | dbg!(1, 2, (3, 4));
| ~~~~~~
error: aborting due to 6 previous errors