about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2024-08-22 13:30:50 +0200
committerUrgau <urgau@numericable.fr>2024-08-22 13:32:01 +0200
commit6a878a9630945cd6a61ad5e83ec5c543c6e8dab7 (patch)
tree409782fb24552677372624491e85e54055adc05f
parente08b80c0fb7667bdcd040761891701e576c42ec8 (diff)
downloadrust-6a878a9630945cd6a61ad5e83ec5c543c6e8dab7.tar.gz
rust-6a878a9630945cd6a61ad5e83ec5c543c6e8dab7.zip
Fix handling of macro arguments within the `dropping_copy_types lint
-rw-r--r--compiler/rustc_lint/src/drop_forget_useless.rs5
-rw-r--r--tests/ui/lint/dropping_copy_types-macros.fixed12
-rw-r--r--tests/ui/lint/dropping_copy_types-macros.rs12
-rw-r--r--tests/ui/lint/dropping_copy_types-macros.stderr21
4 files changed, 48 insertions, 2 deletions
diff --git a/compiler/rustc_lint/src/drop_forget_useless.rs b/compiler/rustc_lint/src/drop_forget_useless.rs
index 2060858cc8a..a9de258e005 100644
--- a/compiler/rustc_lint/src/drop_forget_useless.rs
+++ b/compiler/rustc_lint/src/drop_forget_useless.rs
@@ -151,10 +151,11 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
                     && let Node::Stmt(stmt) = node
                     && let StmtKind::Semi(e) = stmt.kind
                     && e.hir_id == expr.hir_id
+                    && let Some(arg_span) = arg.span.find_ancestor_inside(expr.span)
                 {
                     UseLetUnderscoreIgnoreSuggestion::Suggestion {
-                        start_span: expr.span.shrink_to_lo().until(arg.span),
-                        end_span: arg.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
+                        start_span: expr.span.shrink_to_lo().until(arg_span),
+                        end_span: arg_span.shrink_to_hi().until(expr.span.shrink_to_hi()),
                     }
                 } else {
                     UseLetUnderscoreIgnoreSuggestion::Note
diff --git a/tests/ui/lint/dropping_copy_types-macros.fixed b/tests/ui/lint/dropping_copy_types-macros.fixed
new file mode 100644
index 00000000000..a8ceedadc80
--- /dev/null
+++ b/tests/ui/lint/dropping_copy_types-macros.fixed
@@ -0,0 +1,12 @@
+//@ check-fail
+//@ run-rustfix
+
+#![deny(dropping_copy_types)]
+
+use std::fmt::Write;
+
+fn main() {
+    let mut msg = String::new();
+    let _ = writeln!(&mut msg, "test");
+    //~^ ERROR calls to `std::mem::drop`
+}
diff --git a/tests/ui/lint/dropping_copy_types-macros.rs b/tests/ui/lint/dropping_copy_types-macros.rs
new file mode 100644
index 00000000000..b249b0c868f
--- /dev/null
+++ b/tests/ui/lint/dropping_copy_types-macros.rs
@@ -0,0 +1,12 @@
+//@ check-fail
+//@ run-rustfix
+
+#![deny(dropping_copy_types)]
+
+use std::fmt::Write;
+
+fn main() {
+    let mut msg = String::new();
+    drop(writeln!(&mut msg, "test"));
+    //~^ ERROR calls to `std::mem::drop`
+}
diff --git a/tests/ui/lint/dropping_copy_types-macros.stderr b/tests/ui/lint/dropping_copy_types-macros.stderr
new file mode 100644
index 00000000000..117e9f4fe09
--- /dev/null
+++ b/tests/ui/lint/dropping_copy_types-macros.stderr
@@ -0,0 +1,21 @@
+error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
+  --> $DIR/dropping_copy_types-macros.rs:10:5
+   |
+LL |     drop(writeln!(&mut msg, "test"));
+   |     ^^^^^--------------------------^
+   |          |
+   |          argument has type `Result<(), std::fmt::Error>`
+   |
+note: the lint level is defined here
+  --> $DIR/dropping_copy_types-macros.rs:4:9
+   |
+LL | #![deny(dropping_copy_types)]
+   |         ^^^^^^^^^^^^^^^^^^^
+help: use `let _ = ...` to ignore the expression or result
+   |
+LL -     drop(writeln!(&mut msg, "test"));
+LL +     let _ = writeln!(&mut msg, "test");
+   |
+
+error: aborting due to 1 previous error
+