From 57a464439ec57d33815f75483b3bc6a6b6712325 Mon Sep 17 00:00:00 2001 From: Alex Macleod Date: Wed, 1 Nov 2023 16:19:06 +0000 Subject: Fix `dbg_macro` semi span calculation --- clippy_lints/src/dbg_macro.rs | 33 +---- tests/ui/dbg_macro.rs | 107 --------------- tests/ui/dbg_macro.stderr | 203 ---------------------------- tests/ui/dbg_macro/auxiliary/submodule.rs | 3 + tests/ui/dbg_macro/dbg_macro.rs | 109 +++++++++++++++ tests/ui/dbg_macro/dbg_macro.stderr | 215 ++++++++++++++++++++++++++++++ 6 files changed, 331 insertions(+), 339 deletions(-) delete mode 100644 tests/ui/dbg_macro.rs delete mode 100644 tests/ui/dbg_macro.stderr create mode 100644 tests/ui/dbg_macro/auxiliary/submodule.rs create mode 100644 tests/ui/dbg_macro/dbg_macro.rs create mode 100644 tests/ui/dbg_macro/dbg_macro.stderr diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index 49452136d6f..4774917c7b5 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -6,7 +6,7 @@ use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind, Node}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::{declare_tool_lint, impl_lint_pass}; -use rustc_span::{sym, BytePos, Pos, Span}; +use rustc_span::sym; declare_clippy_lint! { /// ### What it does @@ -31,31 +31,6 @@ declare_clippy_lint! { "`dbg!` macro is intended as a debugging tool" } -/// Gets the span of the statement up to the next semicolon, if and only if the next -/// non-whitespace character actually is a semicolon. -/// E.g. -/// ```rust,ignore -/// -/// dbg!(); -/// ^^^^^^^ this span is returned -/// -/// foo!(dbg!()); -/// no span is returned -/// ``` -fn span_including_semi(cx: &LateContext<'_>, span: Span) -> Option { - let sm = cx.sess().source_map(); - let sf = sm.lookup_source_file(span.hi()); - let src = sf.src.as_ref()?.get(span.hi().to_usize()..)?; - let first_non_whitespace = src.find(|c: char| !c.is_whitespace())?; - - if src.as_bytes()[first_non_whitespace] == b';' { - let hi = span.hi() + BytePos::from_usize(first_non_whitespace + 1); - Some(span.with_hi(hi)) - } else { - None - } -} - #[derive(Copy, Clone)] pub struct DbgMacro { allow_dbg_in_tests: bool, @@ -88,10 +63,10 @@ impl LateLintPass<'_> for DbgMacro { ExprKind::Block(..) => { // If the `dbg!` macro is a "free" statement and not contained within other expressions, // remove the whole statement. - if let Some(Node::Stmt(stmt)) = cx.tcx.hir().find_parent(expr.hir_id) - && let Some(span) = span_including_semi(cx, stmt.span.source_callsite()) + if let Some(Node::Stmt(_)) = cx.tcx.hir().find_parent(expr.hir_id) + && let Some(semi_span) = cx.sess().source_map().mac_call_stmt_semi_span(macro_call.span) { - (span, String::new()) + (macro_call.span.to(semi_span), String::new()) } else { (macro_call.span, String::from("()")) } diff --git a/tests/ui/dbg_macro.rs b/tests/ui/dbg_macro.rs deleted file mode 100644 index 149b0847619..00000000000 --- a/tests/ui/dbg_macro.rs +++ /dev/null @@ -1,107 +0,0 @@ -//@no-rustfix - -#![warn(clippy::dbg_macro)] - -fn foo(n: u32) -> u32 { - if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n } - //~^ ERROR: the `dbg!` macro is intended as a debugging tool - //~| NOTE: `-D clippy::dbg-macro` implied by `-D warnings` -} -fn bar(_: ()) {} - -fn factorial(n: u32) -> u32 { - if dbg!(n <= 1) { - //~^ ERROR: the `dbg!` macro is intended as a debugging tool - dbg!(1) - //~^ ERROR: the `dbg!` macro is intended as a debugging tool - } else { - dbg!(n * factorial(n - 1)) - //~^ ERROR: the `dbg!` macro is intended as a debugging tool - } -} - -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 -} - -fn issue9914() { - macro_rules! foo { - ($x:expr) => { - $x; - }; - } - macro_rules! foo2 { - ($x:expr) => { - $x; - }; - } - macro_rules! expand_to_dbg { - () => { - dbg!(); - }; - } - - dbg!(); - //~^ ERROR: the `dbg!` macro is intended as a debugging tool - #[allow(clippy::let_unit_value)] - let _ = dbg!(); - //~^ ERROR: the `dbg!` macro is intended as a debugging tool - bar(dbg!()); - //~^ ERROR: the `dbg!` macro is intended as a debugging tool - foo!(dbg!()); - //~^ ERROR: the `dbg!` macro is intended as a debugging tool - foo2!(foo!(dbg!())); - //~^ 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, { - dbg!(2); - //~^ ERROR: the `dbg!` macro is intended as a debugging tool - }); -} - -#[test] -pub fn issue8481() { - dbg!(1); - //~^ ERROR: the `dbg!` macro is intended as a debugging tool -} - -#[cfg(test)] -fn foo2() { - dbg!(1); - //~^ ERROR: the `dbg!` macro is intended as a debugging tool -} - -#[cfg(test)] -mod mod1 { - fn func() { - dbg!(1); - //~^ ERROR: the `dbg!` macro is intended as a debugging tool - } -} diff --git a/tests/ui/dbg_macro.stderr b/tests/ui/dbg_macro.stderr deleted file mode 100644 index f45a7ba1fae..00000000000 --- a/tests/ui/dbg_macro.stderr +++ /dev/null @@ -1,203 +0,0 @@ -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:6: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 - --> $DIR/dbg_macro.rs:13:8 - | -LL | if dbg!(n <= 1) { - | ^^^^^^^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | if n <= 1 { - | ~~~~~~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:15:9 - | -LL | dbg!(1) - | ^^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | 1 - | - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:18:9 - | -LL | dbg!(n * factorial(n - 1)) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | n * factorial(n - 1) - | - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:24:5 - | -LL | dbg!(42); - | ^^^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | 42; - | ~~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:26: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 - --> $DIR/dbg_macro.rs:28:14 - | -LL | foo(3) + dbg!(factorial(4)); - | ^^^^^^^^^^^^^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | foo(3) + factorial(4); - | ~~~~~~~~~~~~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:30: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 - --> $DIR/dbg_macro.rs:32:5 - | -LL | dbg!(1, 2, 3, 4, 5); - | ^^^^^^^^^^^^^^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | (1, 2, 3, 4, 5); - | ~~~~~~~~~~~~~~~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:53:5 - | -LL | dbg!(); - | ^^^^^^^ - | -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 - --> $DIR/dbg_macro.rs:56:13 - | -LL | let _ = dbg!(); - | ^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | let _ = (); - | ~~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:58:9 - | -LL | bar(dbg!()); - | ^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | bar(()); - | ~~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:60:10 - | -LL | foo!(dbg!()); - | ^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | foo!(()); - | ~~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:62:16 - | -LL | foo2!(foo!(dbg!())); - | ^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | foo2!(foo!(())); - | ~~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:84:9 - | -LL | dbg!(2); - | ^^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | 2; - | ~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:91:5 - | -LL | dbg!(1); - | ^^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | 1; - | ~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:97:5 - | -LL | dbg!(1); - | ^^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | 1; - | ~ - -error: the `dbg!` macro is intended as a debugging tool - --> $DIR/dbg_macro.rs:104:9 - | -LL | dbg!(1); - | ^^^^^^^ - | -help: remove the invocation before committing it to a version control system - | -LL | 1; - | ~ - -error: aborting due to 18 previous errors - diff --git a/tests/ui/dbg_macro/auxiliary/submodule.rs b/tests/ui/dbg_macro/auxiliary/submodule.rs new file mode 100644 index 00000000000..b1df24737a2 --- /dev/null +++ b/tests/ui/dbg_macro/auxiliary/submodule.rs @@ -0,0 +1,3 @@ +fn f() { + dbg!(); +} diff --git a/tests/ui/dbg_macro/dbg_macro.rs b/tests/ui/dbg_macro/dbg_macro.rs new file mode 100644 index 00000000000..3f4770c63d0 --- /dev/null +++ b/tests/ui/dbg_macro/dbg_macro.rs @@ -0,0 +1,109 @@ +//@no-rustfix + +#![warn(clippy::dbg_macro)] + +#[path = "auxiliary/submodule.rs"] +mod submodule; + +fn foo(n: u32) -> u32 { + if let Some(n) = dbg!(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 dbg!(n <= 1) { + //~^ ERROR: the `dbg!` macro is intended as a debugging tool + dbg!(1) + //~^ ERROR: the `dbg!` macro is intended as a debugging tool + } else { + dbg!(n * factorial(n - 1)) + //~^ ERROR: the `dbg!` macro is intended as a debugging tool + } +} + +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 +} + +fn issue9914() { + macro_rules! foo { + ($x:expr) => { + $x; + }; + } + macro_rules! foo2 { + ($x:expr) => { + $x; + }; + } + macro_rules! expand_to_dbg { + () => { + dbg!(); + }; + } + + dbg!(); + //~^ ERROR: the `dbg!` macro is intended as a debugging tool + #[allow(clippy::let_unit_value)] + let _ = dbg!(); + //~^ ERROR: the `dbg!` macro is intended as a debugging tool + bar(dbg!()); + //~^ ERROR: the `dbg!` macro is intended as a debugging tool + foo!(dbg!()); + //~^ ERROR: the `dbg!` macro is intended as a debugging tool + foo2!(foo!(dbg!())); + //~^ 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, { + dbg!(2); + //~^ ERROR: the `dbg!` macro is intended as a debugging tool + }); +} + +#[test] +pub fn issue8481() { + dbg!(1); + //~^ ERROR: the `dbg!` macro is intended as a debugging tool +} + +#[cfg(test)] +fn foo2() { + dbg!(1); + //~^ ERROR: the `dbg!` macro is intended as a debugging tool +} + +#[cfg(test)] +mod mod1 { + fn func() { + dbg!(1); + //~^ ERROR: the `dbg!` macro is intended as a debugging tool + } +} diff --git a/tests/ui/dbg_macro/dbg_macro.stderr b/tests/ui/dbg_macro/dbg_macro.stderr new file mode 100644 index 00000000000..4d00421c711 --- /dev/null +++ b/tests/ui/dbg_macro/dbg_macro.stderr @@ -0,0 +1,215 @@ +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/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 + --> $DIR/dbg_macro.rs:9:22 + | +LL | if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n } + | ^^^^^^^^^^^^^^^^^^^^^^ + | +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 + --> $DIR/dbg_macro.rs:15:8 + | +LL | if dbg!(n <= 1) { + | ^^^^^^^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | if n <= 1 { + | ~~~~~~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:17:9 + | +LL | dbg!(1) + | ^^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | 1 + | + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:20:9 + | +LL | dbg!(n * factorial(n - 1)) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | n * factorial(n - 1) + | + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:26:5 + | +LL | dbg!(42); + | ^^^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | 42; + | ~~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/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 + --> $DIR/dbg_macro.rs:30:14 + | +LL | foo(3) + dbg!(factorial(4)); + | ^^^^^^^^^^^^^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | foo(3) + factorial(4); + | ~~~~~~~~~~~~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/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 + --> $DIR/dbg_macro.rs:34:5 + | +LL | dbg!(1, 2, 3, 4, 5); + | ^^^^^^^^^^^^^^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | (1, 2, 3, 4, 5); + | ~~~~~~~~~~~~~~~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:55:5 + | +LL | dbg!(); + | ^^^^^^^ + | +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 + --> $DIR/dbg_macro.rs:58:13 + | +LL | let _ = dbg!(); + | ^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | let _ = (); + | ~~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:60:9 + | +LL | bar(dbg!()); + | ^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | bar(()); + | ~~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:62:10 + | +LL | foo!(dbg!()); + | ^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | foo!(()); + | ~~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:64:16 + | +LL | foo2!(foo!(dbg!())); + | ^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | foo2!(foo!(())); + | ~~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:86:9 + | +LL | dbg!(2); + | ^^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | 2; + | ~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:93:5 + | +LL | dbg!(1); + | ^^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | 1; + | ~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:99:5 + | +LL | dbg!(1); + | ^^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | 1; + | ~ + +error: the `dbg!` macro is intended as a debugging tool + --> $DIR/dbg_macro.rs:106:9 + | +LL | dbg!(1); + | ^^^^^^^ + | +help: remove the invocation before committing it to a version control system + | +LL | 1; + | ~ + +error: aborting due to 19 previous errors + -- cgit 1.4.1-3-g733a5