Skip to content

Commit cc607fe

Browse files
committed
don't remove dbg! in arbitrary expressions
1 parent 7bc3da9 commit cc607fe

File tree

3 files changed

+82
-24
lines changed

3 files changed

+82
-24
lines changed

clippy_lints/src/dbg_macro.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use clippy_utils::macros::root_macro_call_first_node;
33
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::{is_in_cfg_test, is_in_test_function};
55
use rustc_errors::Applicability;
6-
use rustc_hir::{Expr, ExprKind};
7-
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_hir::{Expr, ExprKind, Node, Stmt, StmtKind};
7+
use rustc_lint::{LateContext, LateLintPass, LintContext};
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
9-
use rustc_span::sym;
9+
use rustc_span::{sym, Span};
1010

1111
declare_clippy_lint! {
1212
/// ### What it does
@@ -31,6 +31,11 @@ declare_clippy_lint! {
3131
"`dbg!` macro is intended as a debugging tool"
3232
}
3333

34+
fn span_including_semi(cx: &LateContext<'_>, span: Span) -> Span {
35+
let span = cx.sess().source_map().span_extend_to_next_char(span, ';', true);
36+
span.with_hi(span.hi() + rustc_span::BytePos(1))
37+
}
38+
3439
#[derive(Copy, Clone)]
3540
pub struct DbgMacro {
3641
allow_dbg_in_tests: bool,
@@ -55,13 +60,24 @@ impl LateLintPass<'_> for DbgMacro {
5560
return;
5661
}
5762
let mut applicability = Applicability::MachineApplicable;
58-
let suggestion = match expr.peel_drop_temps().kind {
59-
// dbg!()
60-
ExprKind::Block(_, _) => String::new(),
61-
// dbg!(1)
62-
ExprKind::Match(val, ..) => {
63-
snippet_with_applicability(cx, val.span.source_callsite(), "..", &mut applicability).to_string()
63+
64+
let (sugg_span, suggestion) = match expr.peel_drop_temps().kind {
65+
ExprKind::Block(..) => match cx.tcx.hir().find_parent(expr.hir_id) {
66+
// dbg!() as a standalone statement, suggest removing the whole statement entirely
67+
Some(Node::Stmt(
68+
stmt @ Stmt {
69+
kind: StmtKind::Semi(_),
70+
..
71+
},
72+
)) => (span_including_semi(cx, stmt.span.source_callsite()), String::new()),
73+
// empty dbg!() in arbitrary position (e.g. `foo(dbg!())`), suggest replacing with `foo(())`
74+
_ => (macro_call.span, String::from("()")),
6475
},
76+
// dbg!(1)
77+
ExprKind::Match(val, ..) => (
78+
macro_call.span,
79+
snippet_with_applicability(cx, val.span.source_callsite(), "..", &mut applicability).to_string(),
80+
),
6581
// dbg!(2, 3)
6682
ExprKind::Tup(
6783
[
@@ -82,15 +98,15 @@ impl LateLintPass<'_> for DbgMacro {
8298
"..",
8399
&mut applicability,
84100
);
85-
format!("({snippet})")
101+
(macro_call.span, format!("({snippet})"))
86102
},
87103
_ => return,
88104
};
89105

90106
span_lint_and_sugg(
91107
cx,
92108
DBG_MACRO,
93-
macro_call.span,
109+
sugg_span,
94110
"the `dbg!` macro is intended as a debugging tool",
95111
"remove the invocation before committing it to a version control system",
96112
suggestion,

tests/ui/dbg_macro.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
fn foo(n: u32) -> u32 {
55
if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
66
}
7+
fn bar(_: ()) {}
78

89
fn factorial(n: u32) -> u32 {
910
if dbg!(n <= 1) {
@@ -21,6 +22,13 @@ fn main() {
2122
dbg!(1, 2, 3, 4, 5);
2223
}
2324

25+
fn issue9914() {
26+
dbg!();
27+
#[allow(clippy::let_unit_value)]
28+
let _ = dbg!();
29+
bar(dbg!());
30+
}
31+
2432
mod issue7274 {
2533
trait Thing<'b> {
2634
fn foo(&self);

tests/ui/dbg_macro.stderr

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LL | if let Some(n) = n.checked_sub(4) { n } else { n }
1111
| ~~~~~~~~~~~~~~~~
1212

1313
error: the `dbg!` macro is intended as a debugging tool
14-
--> $DIR/dbg_macro.rs:9:8
14+
--> $DIR/dbg_macro.rs:10:8
1515
|
1616
LL | if dbg!(n <= 1) {
1717
| ^^^^^^^^^^^^
@@ -22,7 +22,7 @@ LL | if n <= 1 {
2222
| ~~~~~~
2323

2424
error: the `dbg!` macro is intended as a debugging tool
25-
--> $DIR/dbg_macro.rs:10:9
25+
--> $DIR/dbg_macro.rs:11:9
2626
|
2727
LL | dbg!(1)
2828
| ^^^^^^^
@@ -33,7 +33,7 @@ LL | 1
3333
|
3434

3535
error: the `dbg!` macro is intended as a debugging tool
36-
--> $DIR/dbg_macro.rs:12:9
36+
--> $DIR/dbg_macro.rs:13:9
3737
|
3838
LL | dbg!(n * factorial(n - 1))
3939
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -44,7 +44,7 @@ LL | n * factorial(n - 1)
4444
|
4545

4646
error: the `dbg!` macro is intended as a debugging tool
47-
--> $DIR/dbg_macro.rs:17:5
47+
--> $DIR/dbg_macro.rs:18:5
4848
|
4949
LL | dbg!(42);
5050
| ^^^^^^^^
@@ -55,7 +55,7 @@ LL | 42;
5555
| ~~
5656

5757
error: the `dbg!` macro is intended as a debugging tool
58-
--> $DIR/dbg_macro.rs:18:5
58+
--> $DIR/dbg_macro.rs:19:5
5959
|
6060
LL | dbg!(dbg!(dbg!(42)));
6161
| ^^^^^^^^^^^^^^^^^^^^
@@ -66,7 +66,7 @@ LL | dbg!(dbg!(42));
6666
| ~~~~~~~~~~~~~~
6767

6868
error: the `dbg!` macro is intended as a debugging tool
69-
--> $DIR/dbg_macro.rs:19:14
69+
--> $DIR/dbg_macro.rs:20:14
7070
|
7171
LL | foo(3) + dbg!(factorial(4));
7272
| ^^^^^^^^^^^^^^^^^^
@@ -77,7 +77,7 @@ LL | foo(3) + factorial(4);
7777
| ~~~~~~~~~~~~
7878

7979
error: the `dbg!` macro is intended as a debugging tool
80-
--> $DIR/dbg_macro.rs:20:5
80+
--> $DIR/dbg_macro.rs:21:5
8181
|
8282
LL | dbg!(1, 2, dbg!(3, 4));
8383
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -88,7 +88,7 @@ LL | (1, 2, dbg!(3, 4));
8888
| ~~~~~~~~~~~~~~~~~~
8989

9090
error: the `dbg!` macro is intended as a debugging tool
91-
--> $DIR/dbg_macro.rs:21:5
91+
--> $DIR/dbg_macro.rs:22:5
9292
|
9393
LL | dbg!(1, 2, 3, 4, 5);
9494
| ^^^^^^^^^^^^^^^^^^^
@@ -99,7 +99,41 @@ LL | (1, 2, 3, 4, 5);
9999
| ~~~~~~~~~~~~~~~
100100

101101
error: the `dbg!` macro is intended as a debugging tool
102-
--> $DIR/dbg_macro.rs:41:9
102+
--> $DIR/dbg_macro.rs:26:5
103+
|
104+
LL | dbg!();
105+
| ^^^^^^^
106+
|
107+
help: remove the invocation before committing it to a version control system
108+
|
109+
LL - dbg!();
110+
LL +
111+
|
112+
113+
error: the `dbg!` macro is intended as a debugging tool
114+
--> $DIR/dbg_macro.rs:28:13
115+
|
116+
LL | let _ = dbg!();
117+
| ^^^^^^
118+
|
119+
help: remove the invocation before committing it to a version control system
120+
|
121+
LL | let _ = ();
122+
| ~~
123+
124+
error: the `dbg!` macro is intended as a debugging tool
125+
--> $DIR/dbg_macro.rs:29:9
126+
|
127+
LL | bar(dbg!());
128+
| ^^^^^^
129+
|
130+
help: remove the invocation before committing it to a version control system
131+
|
132+
LL | bar(());
133+
| ~~
134+
135+
error: the `dbg!` macro is intended as a debugging tool
136+
--> $DIR/dbg_macro.rs:49:9
103137
|
104138
LL | dbg!(2);
105139
| ^^^^^^^
@@ -110,7 +144,7 @@ LL | 2;
110144
| ~
111145

112146
error: the `dbg!` macro is intended as a debugging tool
113-
--> $DIR/dbg_macro.rs:47:5
147+
--> $DIR/dbg_macro.rs:55:5
114148
|
115149
LL | dbg!(1);
116150
| ^^^^^^^
@@ -121,7 +155,7 @@ LL | 1;
121155
| ~
122156

123157
error: the `dbg!` macro is intended as a debugging tool
124-
--> $DIR/dbg_macro.rs:52:5
158+
--> $DIR/dbg_macro.rs:60:5
125159
|
126160
LL | dbg!(1);
127161
| ^^^^^^^
@@ -132,7 +166,7 @@ LL | 1;
132166
| ~
133167

134168
error: the `dbg!` macro is intended as a debugging tool
135-
--> $DIR/dbg_macro.rs:58:9
169+
--> $DIR/dbg_macro.rs:66:9
136170
|
137171
LL | dbg!(1);
138172
| ^^^^^^^
@@ -142,5 +176,5 @@ help: remove the invocation before committing it to a version control system
142176
LL | 1;
143177
| ~
144178

145-
error: aborting due to 13 previous errors
179+
error: aborting due to 16 previous errors
146180

0 commit comments

Comments
 (0)