Skip to content

Commit

Permalink
Rollup merge of #129408 - Urgau:macro-arg-drop_copy, r=compiler-errors
Browse files Browse the repository at this point in the history
Fix handling of macro arguments within the `dropping_copy_types` lint

This PR fixes the handling of spans with different context (aka macro arguments) than the primary expression within the different `{drop,forget}ing_copy_types` and `{drop,forget}ing_references` lints.

<details>
<summary>Before</summary>

```
warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
 --> drop_writeln.rs:5:5
  |
5 |     drop(writeln!(&mut msg, "test"));
  |     ^^^^^--------------------------^
  |          |
  |          argument has type `Result<(), std::fmt::Error>`
  |
  = note: `#[warn(dropping_copy_types)]` on by default
help: use `let _ = ...` to ignore the expression or result
 --> /home/[..]/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/macros/mod.rs:688:9
  |
68|         let _ =
  |         ~~~~~~~
```

</details>

<details>
<summary>With this PR</summary>

```
warning: calls to `std::mem::drop` with a value that implements `Copy` does nothing
 --> drop_writeln.rs:5:5
  |
5 |     drop(writeln!(&mut msg, "test"));
  |     ^^^^^--------------------------^
  |          |
  |          argument has type `Result<(), std::fmt::Error>`
  |
  = note: `#[warn(dropping_copy_types)]` on by default
help: use `let _ = ...` to ignore the expression or result
  |
5 -     drop(writeln!(&mut msg, "test"));
5 +     let _ = writeln!(&mut msg, "test");
  |
```

</details>

````@rustbot```` label +L-dropping_copy_types
  • Loading branch information
tgross35 committed Aug 23, 2024
2 parents c61a77a + 6a878a9 commit 4402558
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/rustc_lint/src/drop_forget_useless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/ui/lint/dropping_copy_types-macros.fixed
Original file line number Diff line number Diff line change
@@ -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`
}
12 changes: 12 additions & 0 deletions tests/ui/lint/dropping_copy_types-macros.rs
Original file line number Diff line number Diff line change
@@ -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`
}
21 changes: 21 additions & 0 deletions tests/ui/lint/dropping_copy_types-macros.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4402558

Please sign in to comment.