From 6a878a9630945cd6a61ad5e83ec5c543c6e8dab7 Mon Sep 17 00:00:00 2001 From: Urgau Date: Thu, 22 Aug 2024 13:30:50 +0200 Subject: [PATCH] Fix handling of macro arguments within the `dropping_copy_types lint --- .../rustc_lint/src/drop_forget_useless.rs | 5 +++-- .../ui/lint/dropping_copy_types-macros.fixed | 12 +++++++++++ tests/ui/lint/dropping_copy_types-macros.rs | 12 +++++++++++ .../ui/lint/dropping_copy_types-macros.stderr | 21 +++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/ui/lint/dropping_copy_types-macros.fixed create mode 100644 tests/ui/lint/dropping_copy_types-macros.rs create mode 100644 tests/ui/lint/dropping_copy_types-macros.stderr 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 +