about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-11-02 20:11:32 +0000
committerbors <bors@rust-lang.org>2023-11-02 20:11:32 +0000
commit902c79c654b45ab9b8ae425bd2c1fa0c50a3edf7 (patch)
treef552836f9f032660ebb8b1289d60dc56be90875c
parent09ac14c901abc43bd0d617ae4a44e8a4fed98d9c (diff)
parent57a464439ec57d33815f75483b3bc6a6b6712325 (diff)
downloadrust-902c79c654b45ab9b8ae425bd2c1fa0c50a3edf7.tar.gz
rust-902c79c654b45ab9b8ae425bd2c1fa0c50a3edf7.zip
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
-rw-r--r--clippy_lints/src/dbg_macro.rs33
-rw-r--r--tests/ui/dbg_macro/auxiliary/submodule.rs3
-rw-r--r--tests/ui/dbg_macro/dbg_macro.rs (renamed from tests/ui/dbg_macro.rs)4
-rw-r--r--tests/ui/dbg_macro/dbg_macro.stderr (renamed from tests/ui/dbg_macro.stderr)54
4 files changed, 43 insertions, 51 deletions
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<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)]
 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/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.rs b/tests/ui/dbg_macro/dbg_macro.rs
index 149b0847619..3f4770c63d0 100644
--- a/tests/ui/dbg_macro.rs
+++ b/tests/ui/dbg_macro/dbg_macro.rs
@@ -2,10 +2,12 @@
 
 #![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
-    //~| NOTE: `-D clippy::dbg-macro` implied by `-D warnings`
 }
 fn bar(_: ()) {}
 
diff --git a/tests/ui/dbg_macro.stderr b/tests/ui/dbg_macro/dbg_macro.stderr
index f45a7ba1fae..4d00421c711 100644
--- a/tests/ui/dbg_macro.stderr
+++ b/tests/ui/dbg_macro/dbg_macro.stderr
@@ -1,18 +1,30 @@
 error: the `dbg!` macro is intended as a debugging tool
-  --> $DIR/dbg_macro.rs:6:22
+  --> $DIR/auxiliary/submodule.rs:2:5
    |
-LL |     if let Some(n) = dbg!(n.checked_sub(4)) { n } else { n }
-   |                      ^^^^^^^^^^^^^^^^^^^^^^
+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:13:8
+  --> $DIR/dbg_macro.rs:15:8
    |
 LL |     if dbg!(n <= 1) {
    |        ^^^^^^^^^^^^
@@ -23,7 +35,7 @@ LL |     if n <= 1 {
    |        ~~~~~~
 
 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)
    |         ^^^^^^^
@@ -34,7 +46,7 @@ LL |         1
    |
 
 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))
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -45,7 +57,7 @@ LL |         n * factorial(n - 1)
    |
 
 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);
    |     ^^^^^^^^
@@ -56,7 +68,7 @@ LL |     42;
    |     ~~
 
 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)));
    |     ^^^^^^^^^^^^^^^^^^^^
@@ -67,7 +79,7 @@ LL |     dbg!(dbg!(42));
    |     ~~~~~~~~~~~~~~
 
 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));
    |              ^^^^^^^^^^^^^^^^^^
@@ -78,7 +90,7 @@ LL |     foo(3) + factorial(4);
    |              ~~~~~~~~~~~~
 
 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));
    |     ^^^^^^^^^^^^^^^^^^^^^^
@@ -89,7 +101,7 @@ LL |     (1, 2, dbg!(3, 4));
    |     ~~~~~~~~~~~~~~~~~~
 
 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);
    |     ^^^^^^^^^^^^^^^^^^^
@@ -100,7 +112,7 @@ LL |     (1, 2, 3, 4, 5);
    |     ~~~~~~~~~~~~~~~
 
 error: the `dbg!` macro is intended as a debugging tool
-  --> $DIR/dbg_macro.rs:53:5
+  --> $DIR/dbg_macro.rs:55:5
    |
 LL |     dbg!();
    |     ^^^^^^^
@@ -112,7 +124,7 @@ LL +
    |
 
 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!();
    |             ^^^^^^
@@ -123,7 +135,7 @@ LL |     let _ = ();
    |             ~~
 
 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!());
    |         ^^^^^^
@@ -134,7 +146,7 @@ LL |     bar(());
    |         ~~
 
 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!());
    |          ^^^^^^
@@ -145,7 +157,7 @@ LL |     foo!(());
    |          ~~
 
 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!()));
    |                ^^^^^^
@@ -156,7 +168,7 @@ LL |     foo2!(foo!(()));
    |                ~~
 
 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);
    |         ^^^^^^^
@@ -167,7 +179,7 @@ LL |         2;
    |         ~
 
 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);
    |     ^^^^^^^
@@ -178,7 +190,7 @@ LL |     1;
    |     ~
 
 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);
    |     ^^^^^^^
@@ -189,7 +201,7 @@ LL |     1;
    |     ~
 
 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);
    |         ^^^^^^^
@@ -199,5 +211,5 @@ help: remove the invocation before committing it to a version control system
 LL |         1;
    |         ~
 
-error: aborting due to 18 previous errors
+error: aborting due to 19 previous errors