Fix dbg_macro semi span calculation

This commit is contained in:
Alex Macleod 2023-11-01 16:19:06 +00:00
parent 919f698da0
commit 57a464439e
4 changed files with 43 additions and 51 deletions

View File

@ -6,7 +6,7 @@
use rustc_hir::{Expr, ExprKind, Node}; use rustc_hir::{Expr, ExprKind, Node};
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::{sym, BytePos, Pos, Span}; use rustc_span::sym;
declare_clippy_lint! { declare_clippy_lint! {
/// ### What it does /// ### What it does
@ -31,31 +31,6 @@
"`dbg!` macro is intended as a debugging tool" "`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<Span> {
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)] #[derive(Copy, Clone)]
pub struct DbgMacro { pub struct DbgMacro {
allow_dbg_in_tests: bool, allow_dbg_in_tests: bool,
@ -88,10 +63,10 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
ExprKind::Block(..) => { ExprKind::Block(..) => {
// If the `dbg!` macro is a "free" statement and not contained within other expressions, // If the `dbg!` macro is a "free" statement and not contained within other expressions,
// remove the whole statement. // remove the whole statement.
if let Some(Node::Stmt(stmt)) = cx.tcx.hir().find_parent(expr.hir_id) if let Some(Node::Stmt(_)) = cx.tcx.hir().find_parent(expr.hir_id)
&& let Some(span) = span_including_semi(cx, stmt.span.source_callsite()) && 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 { } else {
(macro_call.span, String::from("()")) (macro_call.span, String::from("()"))
} }

View File

@ -0,0 +1,3 @@
fn f() {
dbg!();
}

View File

@ -2,10 +2,12 @@
#![warn(clippy::dbg_macro)] #![warn(clippy::dbg_macro)]
#[path = "auxiliary/submodule.rs"]
mod submodule;
fn foo(n: u32) -> u32 { fn foo(n: u32) -> u32 {
if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n } if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
//~^ ERROR: the `dbg!` macro is intended as a debugging tool //~^ ERROR: the `dbg!` macro is intended as a debugging tool
//~| NOTE: `-D clippy::dbg-macro` implied by `-D warnings`
} }
fn bar(_: ()) {} fn bar(_: ()) {}

View File

@ -1,18 +1,30 @@
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:6:22 --> $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 } 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 help: remove the invocation before committing it to a version control system
| |
LL | if let Some(n) = n.checked_sub(4) { n } else { n } LL | if let Some(n) = n.checked_sub(4) { n } else { n }
| ~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:13:8 --> $DIR/dbg_macro.rs:15:8
| |
LL | if dbg!(n <= 1) { LL | if dbg!(n <= 1) {
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
@ -23,7 +35,7 @@ LL | if n <= 1 {
| ~~~~~~ | ~~~~~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:15:9 --> $DIR/dbg_macro.rs:17:9
| |
LL | dbg!(1) LL | dbg!(1)
| ^^^^^^^ | ^^^^^^^
@ -34,7 +46,7 @@ LL | 1
| |
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:18:9 --> $DIR/dbg_macro.rs:20:9
| |
LL | dbg!(n * factorial(n - 1)) LL | dbg!(n * factorial(n - 1))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -45,7 +57,7 @@ LL | n * factorial(n - 1)
| |
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:24:5 --> $DIR/dbg_macro.rs:26:5
| |
LL | dbg!(42); LL | dbg!(42);
| ^^^^^^^^ | ^^^^^^^^
@ -56,7 +68,7 @@ LL | 42;
| ~~ | ~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:26:5 --> $DIR/dbg_macro.rs:28:5
| |
LL | dbg!(dbg!(dbg!(42))); LL | dbg!(dbg!(dbg!(42)));
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
@ -67,7 +79,7 @@ LL | dbg!(dbg!(42));
| ~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:28:14 --> $DIR/dbg_macro.rs:30:14
| |
LL | foo(3) + dbg!(factorial(4)); LL | foo(3) + dbg!(factorial(4));
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
@ -78,7 +90,7 @@ LL | foo(3) + factorial(4);
| ~~~~~~~~~~~~ | ~~~~~~~~~~~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:30:5 --> $DIR/dbg_macro.rs:32:5
| |
LL | dbg!(1, 2, dbg!(3, 4)); LL | dbg!(1, 2, dbg!(3, 4));
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
@ -89,7 +101,7 @@ LL | (1, 2, dbg!(3, 4));
| ~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:32:5 --> $DIR/dbg_macro.rs:34:5
| |
LL | dbg!(1, 2, 3, 4, 5); LL | dbg!(1, 2, 3, 4, 5);
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
@ -100,7 +112,7 @@ LL | (1, 2, 3, 4, 5);
| ~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:53:5 --> $DIR/dbg_macro.rs:55:5
| |
LL | dbg!(); LL | dbg!();
| ^^^^^^^ | ^^^^^^^
@ -112,7 +124,7 @@ LL +
| |
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:56:13 --> $DIR/dbg_macro.rs:58:13
| |
LL | let _ = dbg!(); LL | let _ = dbg!();
| ^^^^^^ | ^^^^^^
@ -123,7 +135,7 @@ LL | let _ = ();
| ~~ | ~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:58:9 --> $DIR/dbg_macro.rs:60:9
| |
LL | bar(dbg!()); LL | bar(dbg!());
| ^^^^^^ | ^^^^^^
@ -134,7 +146,7 @@ LL | bar(());
| ~~ | ~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:60:10 --> $DIR/dbg_macro.rs:62:10
| |
LL | foo!(dbg!()); LL | foo!(dbg!());
| ^^^^^^ | ^^^^^^
@ -145,7 +157,7 @@ LL | foo!(());
| ~~ | ~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:62:16 --> $DIR/dbg_macro.rs:64:16
| |
LL | foo2!(foo!(dbg!())); LL | foo2!(foo!(dbg!()));
| ^^^^^^ | ^^^^^^
@ -156,7 +168,7 @@ LL | foo2!(foo!(()));
| ~~ | ~~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:84:9 --> $DIR/dbg_macro.rs:86:9
| |
LL | dbg!(2); LL | dbg!(2);
| ^^^^^^^ | ^^^^^^^
@ -167,7 +179,7 @@ LL | 2;
| ~ | ~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:91:5 --> $DIR/dbg_macro.rs:93:5
| |
LL | dbg!(1); LL | dbg!(1);
| ^^^^^^^ | ^^^^^^^
@ -178,7 +190,7 @@ LL | 1;
| ~ | ~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:97:5 --> $DIR/dbg_macro.rs:99:5
| |
LL | dbg!(1); LL | dbg!(1);
| ^^^^^^^ | ^^^^^^^
@ -189,7 +201,7 @@ LL | 1;
| ~ | ~
error: the `dbg!` macro is intended as a debugging tool error: the `dbg!` macro is intended as a debugging tool
--> $DIR/dbg_macro.rs:104:9 --> $DIR/dbg_macro.rs:106:9
| |
LL | dbg!(1); LL | dbg!(1);
| ^^^^^^^ | ^^^^^^^
@ -199,5 +211,5 @@ help: remove the invocation before committing it to a version control system
LL | 1; LL | 1;
| ~ | ~
error: aborting due to 18 previous errors error: aborting due to 19 previous errors