Skip to content

Commit c97a9c8

Browse files
authored
Rollup merge of #145430 - Urgau:drop_forget_useless-145427, r=lqd
Fix wrong spans with external macros in the `dropping_copy_types` lint This PR fixes some wrong spans manipulations when external macros are involved. Specifically we didn't make sure the spans had the same context, which kind-of make our spans manipulations go wrong and produce weird spans. We fix that by making sure they have the same context. Fixes #145427
2 parents 501837f + ae4eeb9 commit c97a9c8

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

compiler/rustc_lint/src/drop_forget_useless.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ 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)
154+
&& let Some(arg_span) = arg.span.find_ancestor_inside_same_ctxt(expr.span)
155155
{
156156
UseLetUnderscoreIgnoreSuggestion::Suggestion {
157157
start_span: expr.span.shrink_to_lo().until(arg_span),

tests/ui/lint/dropping_copy_types-macros.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ fn main() {
99
let mut msg = String::new();
1010
let _ = writeln!(&mut msg, "test");
1111
//~^ ERROR calls to `std::mem::drop`
12+
13+
let _ = format_args!("a");
14+
//~^ ERROR calls to `std::mem::drop`
1215
}

tests/ui/lint/dropping_copy_types-macros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ fn main() {
99
let mut msg = String::new();
1010
drop(writeln!(&mut msg, "test"));
1111
//~^ ERROR calls to `std::mem::drop`
12+
13+
drop(format_args!("a"));
14+
//~^ ERROR calls to `std::mem::drop`
1215
}

tests/ui/lint/dropping_copy_types-macros.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,19 @@ LL - drop(writeln!(&mut msg, "test"));
1717
LL + let _ = writeln!(&mut msg, "test");
1818
|
1919

20-
error: aborting due to 1 previous error
20+
error: calls to `std::mem::drop` with a value that implements `Copy` does nothing
21+
--> $DIR/dropping_copy_types-macros.rs:13:5
22+
|
23+
LL | drop(format_args!("a"));
24+
| ^^^^^-----------------^
25+
| |
26+
| argument has type `Arguments<'_>`
27+
|
28+
help: use `let _ = ...` to ignore the expression or result
29+
|
30+
LL - drop(format_args!("a"));
31+
LL + let _ = format_args!("a");
32+
|
33+
34+
error: aborting due to 2 previous errors
2135

0 commit comments

Comments
 (0)