Skip to content

Commit 81aca63

Browse files
Rollup merge of rust-lang#129408 - Urgau:macro-arg-drop_copy, r=compiler-errors
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
2 parents 1bbb8d5 + 6a878a9 commit 81aca63

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

compiler/rustc_lint/src/drop_forget_useless.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
151151
&& let Node::Stmt(stmt) = node
152152
&& let StmtKind::Semi(e) = stmt.kind
153153
&& e.hir_id == expr.hir_id
154+
&& let Some(arg_span) = arg.span.find_ancestor_inside(expr.span)
154155
{
155156
UseLetUnderscoreIgnoreSuggestion::Suggestion {
156-
start_span: expr.span.shrink_to_lo().until(arg.span),
157-
end_span: arg.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
157+
start_span: expr.span.shrink_to_lo().until(arg_span),
158+
end_span: arg_span.shrink_to_hi().until(expr.span.shrink_to_hi()),
158159
}
159160
} else {
160161
UseLetUnderscoreIgnoreSuggestion::Note
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ check-fail
2+
//@ run-rustfix
3+
4+
#![deny(dropping_copy_types)]
5+
6+
use std::fmt::Write;
7+
8+
fn main() {
9+
let mut msg = String::new();
10+
let _ = writeln!(&mut msg, "test");
11+
//~^ ERROR calls to `std::mem::drop`
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ check-fail
2+
//@ run-rustfix
3+
4+
#![deny(dropping_copy_types)]
5+
6+
use std::fmt::Write;
7+
8+
fn main() {
9+
let mut msg = String::new();
10+
drop(writeln!(&mut msg, "test"));
11+
//~^ ERROR calls to `std::mem::drop`
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
2+
--> $DIR/dropping_copy_types-macros.rs:10:5
3+
|
4+
LL | drop(writeln!(&mut msg, "test"));
5+
| ^^^^^--------------------------^
6+
| |
7+
| argument has type `Result<(), std::fmt::Error>`
8+
|
9+
note: the lint level is defined here
10+
--> $DIR/dropping_copy_types-macros.rs:4:9
11+
|
12+
LL | #![deny(dropping_copy_types)]
13+
| ^^^^^^^^^^^^^^^^^^^
14+
help: use `let _ = ...` to ignore the expression or result
15+
|
16+
LL - drop(writeln!(&mut msg, "test"));
17+
LL + let _ = writeln!(&mut msg, "test");
18+
|
19+
20+
error: aborting due to 1 previous error
21+

0 commit comments

Comments
 (0)