Skip to content

Commit fc21512

Browse files
authored
Unrolled build for rust-lang#117390
Rollup merge of rust-lang#117390 - chenyukang:yukang-fix-117284-unused-macro, r=estebank Fix unused variables lint issue for args in macro Fixes rust-lang#117284 r? ````@estebank````
2 parents e6e931d + 82f34fd commit fc21512

File tree

5 files changed

+84
-11
lines changed

5 files changed

+84
-11
lines changed

compiler/rustc_passes/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -778,13 +778,16 @@ passes_unused_var_maybe_capture_ref = unused variable: `{$name}`
778778
passes_unused_var_remove_field = unused variable: `{$name}`
779779
passes_unused_var_remove_field_suggestion = try removing the field
780780
781+
passes_unused_variable_args_in_macro = `{$name}` is captured in macro and introduced a unused variable
782+
781783
passes_unused_variable_try_ignore = unused variable: `{$name}`
782784
.suggestion = try ignoring the field
783785
784786
passes_unused_variable_try_prefix = unused variable: `{$name}`
785787
.label = unused variable
786788
.suggestion = if this is intentional, prefix it with an underscore
787789
790+
788791
passes_used_compiler_linker =
789792
`used(compiler)` and `used(linker)` can't be used together
790793

compiler/rustc_passes/src/errors.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -1768,15 +1768,24 @@ pub struct UnusedVariableTryPrefix {
17681768
#[subdiagnostic]
17691769
pub string_interp: Vec<UnusedVariableStringInterp>,
17701770
#[subdiagnostic]
1771-
pub sugg: UnusedVariableTryPrefixSugg,
1771+
pub sugg: UnusedVariableSugg,
1772+
pub name: String,
17721773
}
17731774

17741775
#[derive(Subdiagnostic)]
1775-
#[multipart_suggestion(passes_suggestion, applicability = "machine-applicable")]
1776-
pub struct UnusedVariableTryPrefixSugg {
1777-
#[suggestion_part(code = "_{name}")]
1778-
pub spans: Vec<Span>,
1779-
pub name: String,
1776+
pub enum UnusedVariableSugg {
1777+
#[multipart_suggestion(passes_suggestion, applicability = "machine-applicable")]
1778+
TryPrefixSugg {
1779+
#[suggestion_part(code = "_{name}")]
1780+
spans: Vec<Span>,
1781+
name: String,
1782+
},
1783+
#[help(passes_unused_variable_args_in_macro)]
1784+
NoSugg {
1785+
#[primary_span]
1786+
span: Span,
1787+
name: String,
1788+
},
17801789
}
17811790

17821791
pub struct UnusedVariableStringInterp {

compiler/rustc_passes/src/liveness.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,6 @@ impl<'tcx> Liveness<'_, 'tcx> {
15801580
opt_body: Option<&hir::Body<'_>>,
15811581
) {
15821582
let first_hir_id = hir_ids_and_spans[0].0;
1583-
15841583
if let Some(name) = self.should_warn(var).filter(|name| name != "self") {
15851584
// annoying: for parameters in funcs like `fn(x: i32)
15861585
// {ret}`, there is only one node, so asking about
@@ -1652,11 +1651,29 @@ impl<'tcx> Liveness<'_, 'tcx> {
16521651
},
16531652
);
16541653
} else {
1654+
// #117284, when `pat_span` and `ident_span` have different contexts
1655+
// we can't provide a good suggestion, instead we pointed out the spans from macro
1656+
let from_macro = non_shorthands
1657+
.iter()
1658+
.find(|(_, pat_span, ident_span)| {
1659+
pat_span.ctxt() != ident_span.ctxt() && pat_span.from_expansion()
1660+
})
1661+
.map(|(_, pat_span, _)| *pat_span);
16551662
let non_shorthands = non_shorthands
16561663
.into_iter()
16571664
.map(|(_, _, ident_span)| ident_span)
16581665
.collect::<Vec<_>>();
1666+
16591667
let suggestions = self.string_interp_suggestions(&name, opt_body);
1668+
let sugg = if let Some(span) = from_macro {
1669+
errors::UnusedVariableSugg::NoSugg { span, name: name.clone() }
1670+
} else {
1671+
errors::UnusedVariableSugg::TryPrefixSugg {
1672+
spans: non_shorthands,
1673+
name: name.clone(),
1674+
}
1675+
};
1676+
16601677
self.ir.tcx.emit_spanned_lint(
16611678
lint::builtin::UNUSED_VARIABLES,
16621679
first_hir_id,
@@ -1666,10 +1683,8 @@ impl<'tcx> Liveness<'_, 'tcx> {
16661683
.collect::<Vec<_>>(),
16671684
errors::UnusedVariableTryPrefix {
16681685
label: if !suggestions.is_empty() { Some(pat.span) } else { None },
1669-
sugg: errors::UnusedVariableTryPrefixSugg {
1670-
spans: non_shorthands,
1671-
name,
1672-
},
1686+
name,
1687+
sugg,
16731688
string_interp: suggestions,
16741689
},
16751690
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![deny(unused_variables)]
2+
macro_rules! make_var {
3+
($struct:ident, $var:ident) => {
4+
let $var = $struct.$var;
5+
};
6+
}
7+
8+
#[allow(unused)]
9+
struct MyStruct {
10+
var: i32,
11+
}
12+
13+
fn main() {
14+
let s = MyStruct { var: 42 };
15+
make_var!(s, var); //~ ERROR unused variable: `var`
16+
let a = 1; //~ ERROR unused variable: `a`
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: unused variable: `var`
2+
--> $DIR/issue-117284-arg-in-macro.rs:15:18
3+
|
4+
LL | make_var!(s, var);
5+
| ^^^
6+
|
7+
help: `var` is captured in macro and introduced a unused variable
8+
--> $DIR/issue-117284-arg-in-macro.rs:4:13
9+
|
10+
LL | let $var = $struct.$var;
11+
| ^^^^
12+
...
13+
LL | make_var!(s, var);
14+
| ----------------- in this macro invocation
15+
note: the lint level is defined here
16+
--> $DIR/issue-117284-arg-in-macro.rs:1:9
17+
|
18+
LL | #![deny(unused_variables)]
19+
| ^^^^^^^^^^^^^^^^
20+
= note: this error originates in the macro `make_var` (in Nightly builds, run with -Z macro-backtrace for more info)
21+
22+
error: unused variable: `a`
23+
--> $DIR/issue-117284-arg-in-macro.rs:16:9
24+
|
25+
LL | let a = 1;
26+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
27+
28+
error: aborting due to 2 previous errors
29+

0 commit comments

Comments
 (0)