Skip to content

Commit ceaa5ea

Browse files
authored
Rollup merge of #143030 - Urgau:issue-143025, r=SparrowLii
Fix suggestion spans inside macros for the `unused_must_use` lint This PR fixes the suggestion spans inside macros for the `unused_must_use` lint by trying to find the oldest ancestor span. Fixes #143025
2 parents a43d7cb + 3175fb2 commit ceaa5ea

File tree

5 files changed

+178
-5
lines changed

5 files changed

+178
-5
lines changed

compiler/rustc_lint/src/unused.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
183183
let mut op_warned = false;
184184

185185
if let Some(must_use_op) = must_use_op {
186+
let span = expr.span.find_oldest_ancestor_in_same_ctxt();
186187
cx.emit_span_lint(
187188
UNUSED_MUST_USE,
188189
expr.span,
@@ -191,11 +192,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
191192
label: expr.span,
192193
suggestion: if expr_is_from_block {
193194
UnusedOpSuggestion::BlockTailExpr {
194-
before_span: expr.span.shrink_to_lo(),
195-
after_span: expr.span.shrink_to_hi(),
195+
before_span: span.shrink_to_lo(),
196+
after_span: span.shrink_to_hi(),
196197
}
197198
} else {
198-
UnusedOpSuggestion::NormalExpr { span: expr.span.shrink_to_lo() }
199+
UnusedOpSuggestion::NormalExpr { span: span.shrink_to_lo() }
199200
},
200201
},
201202
);
@@ -508,9 +509,10 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
508509
);
509510
}
510511
MustUsePath::Def(span, def_id, reason) => {
512+
let span = span.find_oldest_ancestor_in_same_ctxt();
511513
cx.emit_span_lint(
512514
UNUSED_MUST_USE,
513-
*span,
515+
span,
514516
UnusedDef {
515517
pre: descr_pre,
516518
post: descr_post,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Makes sure the suggestions of the `unused_must_use` lint are not inside
2+
//
3+
// See <https://github.com/rust-lang/rust/issues/143025>
4+
5+
//@ check-pass
6+
//@ run-rustfix
7+
8+
#![expect(unused_macros)]
9+
#![warn(unused_must_use)]
10+
11+
fn main() {
12+
{
13+
macro_rules! cmp {
14+
($a:tt, $b:tt) => {
15+
$a == $b
16+
};
17+
}
18+
19+
// FIXME(Urgau): For some unknown reason the spans we get are not
20+
// recorded to be from any expansions, preventing us from either
21+
// suggesting in front of the macro or not at all.
22+
// cmp!(1, 1);
23+
}
24+
25+
{
26+
macro_rules! cmp {
27+
($a:ident, $b:ident) => {
28+
$a == $b
29+
}; //~^ WARN unused comparison that must be used
30+
}
31+
32+
let a = 1;
33+
let b = 1;
34+
let _ = cmp!(a, b);
35+
//~^ SUGGESTION let _
36+
}
37+
38+
{
39+
macro_rules! cmp {
40+
($a:expr, $b:expr) => {
41+
$a == $b
42+
}; //~^ WARN unused comparison that must be used
43+
}
44+
45+
let _ = cmp!(1, 1);
46+
//~^ SUGGESTION let _
47+
}
48+
49+
{
50+
macro_rules! cmp {
51+
($a:tt, $b:tt) => {
52+
$a.eq(&$b)
53+
};
54+
}
55+
56+
let _ = cmp!(1, 1);
57+
//~^ WARN unused return value
58+
//~| SUGGESTION let _
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Makes sure the suggestions of the `unused_must_use` lint are not inside
2+
//
3+
// See <https://github.com/rust-lang/rust/issues/143025>
4+
5+
//@ check-pass
6+
//@ run-rustfix
7+
8+
#![expect(unused_macros)]
9+
#![warn(unused_must_use)]
10+
11+
fn main() {
12+
{
13+
macro_rules! cmp {
14+
($a:tt, $b:tt) => {
15+
$a == $b
16+
};
17+
}
18+
19+
// FIXME(Urgau): For some unknown reason the spans we get are not
20+
// recorded to be from any expansions, preventing us from either
21+
// suggesting in front of the macro or not at all.
22+
// cmp!(1, 1);
23+
}
24+
25+
{
26+
macro_rules! cmp {
27+
($a:ident, $b:ident) => {
28+
$a == $b
29+
}; //~^ WARN unused comparison that must be used
30+
}
31+
32+
let a = 1;
33+
let b = 1;
34+
cmp!(a, b);
35+
//~^ SUGGESTION let _
36+
}
37+
38+
{
39+
macro_rules! cmp {
40+
($a:expr, $b:expr) => {
41+
$a == $b
42+
}; //~^ WARN unused comparison that must be used
43+
}
44+
45+
cmp!(1, 1);
46+
//~^ SUGGESTION let _
47+
}
48+
49+
{
50+
macro_rules! cmp {
51+
($a:tt, $b:tt) => {
52+
$a.eq(&$b)
53+
};
54+
}
55+
56+
cmp!(1, 1);
57+
//~^ WARN unused return value
58+
//~| SUGGESTION let _
59+
}
60+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
warning: unused comparison that must be used
2+
--> $DIR/must-use-macros.rs:28:17
3+
|
4+
LL | $a == $b
5+
| ^^^^^^^^ the comparison produces a value
6+
...
7+
LL | cmp!(a, b);
8+
| ---------- in this macro invocation
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/must-use-macros.rs:9:9
12+
|
13+
LL | #![warn(unused_must_use)]
14+
| ^^^^^^^^^^^^^^^
15+
= note: this warning originates in the macro `cmp` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: use `let _ = ...` to ignore the resulting value
17+
|
18+
LL | let _ = cmp!(a, b);
19+
| +++++++
20+
21+
warning: unused comparison that must be used
22+
--> $DIR/must-use-macros.rs:41:17
23+
|
24+
LL | $a == $b
25+
| ^^^^^^^^ the comparison produces a value
26+
...
27+
LL | cmp!(1, 1);
28+
| ---------- in this macro invocation
29+
|
30+
= note: this warning originates in the macro `cmp` (in Nightly builds, run with -Z macro-backtrace for more info)
31+
help: use `let _ = ...` to ignore the resulting value
32+
|
33+
LL | let _ = cmp!(1, 1);
34+
| +++++++
35+
36+
warning: unused return value of `std::cmp::PartialEq::eq` that must be used
37+
--> $DIR/must-use-macros.rs:56:9
38+
|
39+
LL | cmp!(1, 1);
40+
| ^^^^^^^^^^
41+
|
42+
help: use `let _ = ...` to ignore the resulting value
43+
|
44+
LL | let _ = cmp!(1, 1);
45+
| +++++++
46+
47+
warning: 3 warnings emitted
48+

tests/ui/macros/must-use-in-macro-55516.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ LL | write!(&mut example, "{}", 42);
77
= note: this `Result` may be an `Err` variant, which should be handled
88
= note: `-W unused-must-use` implied by `-W unused`
99
= help: to override `-W unused` add `#[allow(unused_must_use)]`
10-
= note: this warning originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
10+
help: use `let _ = ...` to ignore the resulting value
11+
|
12+
LL | let _ = write!(&mut example, "{}", 42);
13+
| +++++++
1114

1215
warning: 1 warning emitted
1316

0 commit comments

Comments
 (0)