Auto merge of #8500 - rust-lang:testless-dbg-macro, r=Manishearth

Omit dbg_macro in test code

This fixes #8481.

---

changelog: none
This commit is contained in:
bors 2022-03-03 18:32:07 +00:00
commit 53189ad190
3 changed files with 21 additions and 10 deletions

View File

@ -1,4 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_in_test_function;
use clippy_utils::macros::root_macro_call_first_node;
use clippy_utils::source::snippet_with_applicability;
use rustc_errors::Applicability;
@ -35,6 +36,10 @@ 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) {
return;
}
let mut applicability = Applicability::MachineApplicable;
let suggestion = match expr.peel_drop_temps().kind {
// dbg!()

View File

@ -1,3 +1,4 @@
// compile-flags: --test
#![warn(clippy::dbg_macro)]
fn foo(n: u32) -> u32 {
@ -40,3 +41,8 @@ mod issue7274 {
dbg!(2);
});
}
#[test]
pub fn issue8481() {
dbg!(1);
}

View File

@ -1,5 +1,5 @@
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:4:22
--> $DIR/dbg_macro.rs:5:22
|
LL | if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
| ^^^^^^^^^^^^^^^^^^^^^^
@ -11,7 +11,7 @@ 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:8:8
--> $DIR/dbg_macro.rs:9:8
|
LL | if dbg!(n <= 1) {
| ^^^^^^^^^^^^
@ -22,7 +22,7 @@ LL | if n <= 1 {
| ~~~~~~
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:9:9
--> $DIR/dbg_macro.rs:10:9
|
LL | dbg!(1)
| ^^^^^^^
@ -33,7 +33,7 @@ LL | 1
|
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:11:9
--> $DIR/dbg_macro.rs:12:9
|
LL | dbg!(n * factorial(n - 1))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -44,7 +44,7 @@ LL | n * factorial(n - 1)
|
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:16:5
--> $DIR/dbg_macro.rs:17:5
|
LL | dbg!(42);
| ^^^^^^^^
@ -55,7 +55,7 @@ LL | 42;
| ~~
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:17:5
--> $DIR/dbg_macro.rs:18:5
|
LL | dbg!(dbg!(dbg!(42)));
| ^^^^^^^^^^^^^^^^^^^^
@ -66,7 +66,7 @@ LL | dbg!(dbg!(42));
| ~~~~~~~~~~~~~~
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:18:14
--> $DIR/dbg_macro.rs:19:14
|
LL | foo(3) + dbg!(factorial(4));
| ^^^^^^^^^^^^^^^^^^
@ -77,7 +77,7 @@ LL | foo(3) + factorial(4);
| ~~~~~~~~~~~~
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:19:5
--> $DIR/dbg_macro.rs:20:5
|
LL | dbg!(1, 2, dbg!(3, 4));
| ^^^^^^^^^^^^^^^^^^^^^^
@ -88,7 +88,7 @@ LL | (1, 2, dbg!(3, 4));
| ~~~~~~~~~~~~~~~~~~
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:20:5
--> $DIR/dbg_macro.rs:21:5
|
LL | dbg!(1, 2, 3, 4, 5);
| ^^^^^^^^^^^^^^^^^^^
@ -99,7 +99,7 @@ LL | (1, 2, 3, 4, 5);
| ~~~~~~~~~~~~~~~
error: `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:40:9
--> $DIR/dbg_macro.rs:41:9
|
LL | dbg!(2);
| ^^^^^^^