Skip to content

Commit 902c79c

Browse files
committed
Auto merge of #11743 - Alexendoo:dbg-macro-stmt-span, r=xFrednet
Fix `dbg_macro` semi span calculation `span_including_semi` was using a `BytePos` to index into a file's source which happened to work because the root file of the test started at `BytePos` 0, it didn't work for other files changelog: none
2 parents 09ac14c + 57a4644 commit 902c79c

File tree

4 files changed

+43
-51
lines changed

4 files changed

+43
-51
lines changed

clippy_lints/src/dbg_macro.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind, Node};
77
use rustc_lint::{LateContext, LateLintPass, LintContext};
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
9-
use rustc_span::{sym, BytePos, Pos, Span};
9+
use rustc_span::sym;
1010

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

34-
/// Gets the span of the statement up to the next semicolon, if and only if the next
35-
/// non-whitespace character actually is a semicolon.
36-
/// E.g.
37-
/// ```rust,ignore
38-
///
39-
/// dbg!();
40-
/// ^^^^^^^ this span is returned
41-
///
42-
/// foo!(dbg!());
43-
/// no span is returned
44-
/// ```
45-
fn span_including_semi(cx: &LateContext<'_>, span: Span) -> Option<Span> {
46-
let sm = cx.sess().source_map();
47-
let sf = sm.lookup_source_file(span.hi());
48-
let src = sf.src.as_ref()?.get(span.hi().to_usize()..)?;
49-
let first_non_whitespace = src.find(|c: char| !c.is_whitespace())?;
50-
51-
if src.as_bytes()[first_non_whitespace] == b';' {
52-
let hi = span.hi() + BytePos::from_usize(first_non_whitespace + 1);
53-
Some(span.with_hi(hi))
54-
} else {
55-
None
56-
}
57-
}
58-
5934
#[derive(Copy, Clone)]
6035
pub struct DbgMacro {
6136
allow_dbg_in_tests: bool,
@@ -88,10 +63,10 @@ impl LateLintPass<'_> for DbgMacro {
8863
ExprKind::Block(..) => {
8964
// If the `dbg!` macro is a "free" statement and not contained within other expressions,
9065
// remove the whole statement.
91-
if let Some(Node::Stmt(stmt)) = cx.tcx.hir().find_parent(expr.hir_id)
92-
&& let Some(span) = span_including_semi(cx, stmt.span.source_callsite())
66+
if let Some(Node::Stmt(_)) = cx.tcx.hir().find_parent(expr.hir_id)
67+
&& let Some(semi_span) = cx.sess().source_map().mac_call_stmt_semi_span(macro_call.span)
9368
{
94-
(span, String::new())
69+
(macro_call.span.to(semi_span), String::new())
9570
} else {
9671
(macro_call.span, String::from("()"))
9772
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn f() {
2+
dbg!();
3+
}

tests/ui/dbg_macro.rs renamed to tests/ui/dbg_macro/dbg_macro.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
#![warn(clippy::dbg_macro)]
44

5+
#[path = "auxiliary/submodule.rs"]
6+
mod submodule;
7+
58
fn foo(n: u32) -> u32 {
69
if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
710
//~^ ERROR: the `dbg!` macro is intended as a debugging tool
8-
//~| NOTE: `-D clippy::dbg-macro` implied by `-D warnings`
911
}
1012
fn bar(_: ()) {}
1113

tests/ui/dbg_macro.stderr renamed to tests/ui/dbg_macro/dbg_macro.stderr

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
error: the `dbg!` macro is intended as a debugging tool
2-
--> $DIR/dbg_macro.rs:6:22
2+
--> $DIR/auxiliary/submodule.rs:2:5
33
|
4-
LL | if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
5-
| ^^^^^^^^^^^^^^^^^^^^^^
4+
LL | dbg!();
5+
| ^^^^^^^
66
|
77
= note: `-D clippy::dbg-macro` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::dbg_macro)]`
99
help: remove the invocation before committing it to a version control system
1010
|
11+
LL - dbg!();
12+
LL +
13+
|
14+
15+
error: the `dbg!` macro is intended as a debugging tool
16+
--> $DIR/dbg_macro.rs:9:22
17+
|
18+
LL | if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
19+
| ^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
help: remove the invocation before committing it to a version control system
22+
|
1123
LL | if let Some(n) = n.checked_sub(4) { n } else { n }
1224
| ~~~~~~~~~~~~~~~~
1325

1426
error: the `dbg!` macro is intended as a debugging tool
15-
--> $DIR/dbg_macro.rs:13:8
27+
--> $DIR/dbg_macro.rs:15:8
1628
|
1729
LL | if dbg!(n <= 1) {
1830
| ^^^^^^^^^^^^
@@ -23,7 +35,7 @@ LL | if n <= 1 {
2335
| ~~~~~~
2436

2537
error: the `dbg!` macro is intended as a debugging tool
26-
--> $DIR/dbg_macro.rs:15:9
38+
--> $DIR/dbg_macro.rs:17:9
2739
|
2840
LL | dbg!(1)
2941
| ^^^^^^^
@@ -34,7 +46,7 @@ LL | 1
3446
|
3547

3648
error: the `dbg!` macro is intended as a debugging tool
37-
--> $DIR/dbg_macro.rs:18:9
49+
--> $DIR/dbg_macro.rs:20:9
3850
|
3951
LL | dbg!(n * factorial(n - 1))
4052
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -45,7 +57,7 @@ LL | n * factorial(n - 1)
4557
|
4658

4759
error: the `dbg!` macro is intended as a debugging tool
48-
--> $DIR/dbg_macro.rs:24:5
60+
--> $DIR/dbg_macro.rs:26:5
4961
|
5062
LL | dbg!(42);
5163
| ^^^^^^^^
@@ -56,7 +68,7 @@ LL | 42;
5668
| ~~
5769

5870
error: the `dbg!` macro is intended as a debugging tool
59-
--> $DIR/dbg_macro.rs:26:5
71+
--> $DIR/dbg_macro.rs:28:5
6072
|
6173
LL | dbg!(dbg!(dbg!(42)));
6274
| ^^^^^^^^^^^^^^^^^^^^
@@ -67,7 +79,7 @@ LL | dbg!(dbg!(42));
6779
| ~~~~~~~~~~~~~~
6880

6981
error: the `dbg!` macro is intended as a debugging tool
70-
--> $DIR/dbg_macro.rs:28:14
82+
--> $DIR/dbg_macro.rs:30:14
7183
|
7284
LL | foo(3) + dbg!(factorial(4));
7385
| ^^^^^^^^^^^^^^^^^^
@@ -78,7 +90,7 @@ LL | foo(3) + factorial(4);
7890
| ~~~~~~~~~~~~
7991

8092
error: the `dbg!` macro is intended as a debugging tool
81-
--> $DIR/dbg_macro.rs:30:5
93+
--> $DIR/dbg_macro.rs:32:5
8294
|
8395
LL | dbg!(1, 2, dbg!(3, 4));
8496
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -89,7 +101,7 @@ LL | (1, 2, dbg!(3, 4));
89101
| ~~~~~~~~~~~~~~~~~~
90102

91103
error: the `dbg!` macro is intended as a debugging tool
92-
--> $DIR/dbg_macro.rs:32:5
104+
--> $DIR/dbg_macro.rs:34:5
93105
|
94106
LL | dbg!(1, 2, 3, 4, 5);
95107
| ^^^^^^^^^^^^^^^^^^^
@@ -100,7 +112,7 @@ LL | (1, 2, 3, 4, 5);
100112
| ~~~~~~~~~~~~~~~
101113

102114
error: the `dbg!` macro is intended as a debugging tool
103-
--> $DIR/dbg_macro.rs:53:5
115+
--> $DIR/dbg_macro.rs:55:5
104116
|
105117
LL | dbg!();
106118
| ^^^^^^^
@@ -112,7 +124,7 @@ LL +
112124
|
113125

114126
error: the `dbg!` macro is intended as a debugging tool
115-
--> $DIR/dbg_macro.rs:56:13
127+
--> $DIR/dbg_macro.rs:58:13
116128
|
117129
LL | let _ = dbg!();
118130
| ^^^^^^
@@ -123,7 +135,7 @@ LL | let _ = ();
123135
| ~~
124136

125137
error: the `dbg!` macro is intended as a debugging tool
126-
--> $DIR/dbg_macro.rs:58:9
138+
--> $DIR/dbg_macro.rs:60:9
127139
|
128140
LL | bar(dbg!());
129141
| ^^^^^^
@@ -134,7 +146,7 @@ LL | bar(());
134146
| ~~
135147

136148
error: the `dbg!` macro is intended as a debugging tool
137-
--> $DIR/dbg_macro.rs:60:10
149+
--> $DIR/dbg_macro.rs:62:10
138150
|
139151
LL | foo!(dbg!());
140152
| ^^^^^^
@@ -145,7 +157,7 @@ LL | foo!(());
145157
| ~~
146158

147159
error: the `dbg!` macro is intended as a debugging tool
148-
--> $DIR/dbg_macro.rs:62:16
160+
--> $DIR/dbg_macro.rs:64:16
149161
|
150162
LL | foo2!(foo!(dbg!()));
151163
| ^^^^^^
@@ -156,7 +168,7 @@ LL | foo2!(foo!(()));
156168
| ~~
157169

158170
error: the `dbg!` macro is intended as a debugging tool
159-
--> $DIR/dbg_macro.rs:84:9
171+
--> $DIR/dbg_macro.rs:86:9
160172
|
161173
LL | dbg!(2);
162174
| ^^^^^^^
@@ -167,7 +179,7 @@ LL | 2;
167179
| ~
168180

169181
error: the `dbg!` macro is intended as a debugging tool
170-
--> $DIR/dbg_macro.rs:91:5
182+
--> $DIR/dbg_macro.rs:93:5
171183
|
172184
LL | dbg!(1);
173185
| ^^^^^^^
@@ -178,7 +190,7 @@ LL | 1;
178190
| ~
179191

180192
error: the `dbg!` macro is intended as a debugging tool
181-
--> $DIR/dbg_macro.rs:97:5
193+
--> $DIR/dbg_macro.rs:99:5
182194
|
183195
LL | dbg!(1);
184196
| ^^^^^^^
@@ -189,7 +201,7 @@ LL | 1;
189201
| ~
190202

191203
error: the `dbg!` macro is intended as a debugging tool
192-
--> $DIR/dbg_macro.rs:104:9
204+
--> $DIR/dbg_macro.rs:106:9
193205
|
194206
LL | dbg!(1);
195207
| ^^^^^^^
@@ -199,5 +211,5 @@ help: remove the invocation before committing it to a version control system
199211
LL | 1;
200212
| ~
201213

202-
error: aborting due to 18 previous errors
214+
error: aborting due to 19 previous errors
203215

0 commit comments

Comments
 (0)