mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-06 23:17:37 +00:00
Rollup merge of #117390 - chenyukang:yukang-fix-117284-unused-macro, r=estebank
Fix unused variables lint issue for args in macro Fixes #117284 r? ````@estebank````
This commit is contained in:
commit
02d32d2bc2
@ -778,6 +778,8 @@ passes_unused_var_maybe_capture_ref = unused variable: `{$name}`
|
|||||||
passes_unused_var_remove_field = unused variable: `{$name}`
|
passes_unused_var_remove_field = unused variable: `{$name}`
|
||||||
passes_unused_var_remove_field_suggestion = try removing the field
|
passes_unused_var_remove_field_suggestion = try removing the field
|
||||||
|
|
||||||
|
passes_unused_variable_args_in_macro = `{$name}` is captured in macro and introduced a unused variable
|
||||||
|
|
||||||
passes_unused_variable_try_ignore = unused variable: `{$name}`
|
passes_unused_variable_try_ignore = unused variable: `{$name}`
|
||||||
.suggestion = try ignoring the field
|
.suggestion = try ignoring the field
|
||||||
|
|
||||||
@ -785,6 +787,7 @@ passes_unused_variable_try_prefix = unused variable: `{$name}`
|
|||||||
.label = unused variable
|
.label = unused variable
|
||||||
.suggestion = if this is intentional, prefix it with an underscore
|
.suggestion = if this is intentional, prefix it with an underscore
|
||||||
|
|
||||||
|
|
||||||
passes_used_compiler_linker =
|
passes_used_compiler_linker =
|
||||||
`used(compiler)` and `used(linker)` can't be used together
|
`used(compiler)` and `used(linker)` can't be used together
|
||||||
|
|
||||||
|
@ -1768,15 +1768,24 @@ pub struct UnusedVariableTryPrefix {
|
|||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
pub string_interp: Vec<UnusedVariableStringInterp>,
|
pub string_interp: Vec<UnusedVariableStringInterp>,
|
||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
pub sugg: UnusedVariableTryPrefixSugg,
|
pub sugg: UnusedVariableSugg,
|
||||||
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subdiagnostic)]
|
#[derive(Subdiagnostic)]
|
||||||
#[multipart_suggestion(passes_suggestion, applicability = "machine-applicable")]
|
pub enum UnusedVariableSugg {
|
||||||
pub struct UnusedVariableTryPrefixSugg {
|
#[multipart_suggestion(passes_suggestion, applicability = "machine-applicable")]
|
||||||
#[suggestion_part(code = "_{name}")]
|
TryPrefixSugg {
|
||||||
pub spans: Vec<Span>,
|
#[suggestion_part(code = "_{name}")]
|
||||||
pub name: String,
|
spans: Vec<Span>,
|
||||||
|
name: String,
|
||||||
|
},
|
||||||
|
#[help(passes_unused_variable_args_in_macro)]
|
||||||
|
NoSugg {
|
||||||
|
#[primary_span]
|
||||||
|
span: Span,
|
||||||
|
name: String,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct UnusedVariableStringInterp {
|
pub struct UnusedVariableStringInterp {
|
||||||
|
@ -1580,7 +1580,6 @@ impl<'tcx> Liveness<'_, 'tcx> {
|
|||||||
opt_body: Option<&hir::Body<'_>>,
|
opt_body: Option<&hir::Body<'_>>,
|
||||||
) {
|
) {
|
||||||
let first_hir_id = hir_ids_and_spans[0].0;
|
let first_hir_id = hir_ids_and_spans[0].0;
|
||||||
|
|
||||||
if let Some(name) = self.should_warn(var).filter(|name| name != "self") {
|
if let Some(name) = self.should_warn(var).filter(|name| name != "self") {
|
||||||
// annoying: for parameters in funcs like `fn(x: i32)
|
// annoying: for parameters in funcs like `fn(x: i32)
|
||||||
// {ret}`, there is only one node, so asking about
|
// {ret}`, there is only one node, so asking about
|
||||||
@ -1652,11 +1651,29 @@ impl<'tcx> Liveness<'_, 'tcx> {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
// #117284, when `pat_span` and `ident_span` have different contexts
|
||||||
|
// we can't provide a good suggestion, instead we pointed out the spans from macro
|
||||||
|
let from_macro = non_shorthands
|
||||||
|
.iter()
|
||||||
|
.find(|(_, pat_span, ident_span)| {
|
||||||
|
pat_span.ctxt() != ident_span.ctxt() && pat_span.from_expansion()
|
||||||
|
})
|
||||||
|
.map(|(_, pat_span, _)| *pat_span);
|
||||||
let non_shorthands = non_shorthands
|
let non_shorthands = non_shorthands
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(_, _, ident_span)| ident_span)
|
.map(|(_, _, ident_span)| ident_span)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let suggestions = self.string_interp_suggestions(&name, opt_body);
|
let suggestions = self.string_interp_suggestions(&name, opt_body);
|
||||||
|
let sugg = if let Some(span) = from_macro {
|
||||||
|
errors::UnusedVariableSugg::NoSugg { span, name: name.clone() }
|
||||||
|
} else {
|
||||||
|
errors::UnusedVariableSugg::TryPrefixSugg {
|
||||||
|
spans: non_shorthands,
|
||||||
|
name: name.clone(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
self.ir.tcx.emit_spanned_lint(
|
self.ir.tcx.emit_spanned_lint(
|
||||||
lint::builtin::UNUSED_VARIABLES,
|
lint::builtin::UNUSED_VARIABLES,
|
||||||
first_hir_id,
|
first_hir_id,
|
||||||
@ -1666,10 +1683,8 @@ impl<'tcx> Liveness<'_, 'tcx> {
|
|||||||
.collect::<Vec<_>>(),
|
.collect::<Vec<_>>(),
|
||||||
errors::UnusedVariableTryPrefix {
|
errors::UnusedVariableTryPrefix {
|
||||||
label: if !suggestions.is_empty() { Some(pat.span) } else { None },
|
label: if !suggestions.is_empty() { Some(pat.span) } else { None },
|
||||||
sugg: errors::UnusedVariableTryPrefixSugg {
|
name,
|
||||||
spans: non_shorthands,
|
sugg,
|
||||||
name,
|
|
||||||
},
|
|
||||||
string_interp: suggestions,
|
string_interp: suggestions,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
17
tests/ui/lint/unused/issue-117284-arg-in-macro.rs
Normal file
17
tests/ui/lint/unused/issue-117284-arg-in-macro.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#![deny(unused_variables)]
|
||||||
|
macro_rules! make_var {
|
||||||
|
($struct:ident, $var:ident) => {
|
||||||
|
let $var = $struct.$var;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
struct MyStruct {
|
||||||
|
var: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let s = MyStruct { var: 42 };
|
||||||
|
make_var!(s, var); //~ ERROR unused variable: `var`
|
||||||
|
let a = 1; //~ ERROR unused variable: `a`
|
||||||
|
}
|
29
tests/ui/lint/unused/issue-117284-arg-in-macro.stderr
Normal file
29
tests/ui/lint/unused/issue-117284-arg-in-macro.stderr
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
error: unused variable: `var`
|
||||||
|
--> $DIR/issue-117284-arg-in-macro.rs:15:18
|
||||||
|
|
|
||||||
|
LL | make_var!(s, var);
|
||||||
|
| ^^^
|
||||||
|
|
|
||||||
|
help: `var` is captured in macro and introduced a unused variable
|
||||||
|
--> $DIR/issue-117284-arg-in-macro.rs:4:13
|
||||||
|
|
|
||||||
|
LL | let $var = $struct.$var;
|
||||||
|
| ^^^^
|
||||||
|
...
|
||||||
|
LL | make_var!(s, var);
|
||||||
|
| ----------------- in this macro invocation
|
||||||
|
note: the lint level is defined here
|
||||||
|
--> $DIR/issue-117284-arg-in-macro.rs:1:9
|
||||||
|
|
|
||||||
|
LL | #![deny(unused_variables)]
|
||||||
|
| ^^^^^^^^^^^^^^^^
|
||||||
|
= note: this error originates in the macro `make_var` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: unused variable: `a`
|
||||||
|
--> $DIR/issue-117284-arg-in-macro.rs:16:9
|
||||||
|
|
|
||||||
|
LL | let a = 1;
|
||||||
|
| ^ help: if this is intentional, prefix it with an underscore: `_a`
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
Loading…
Reference in New Issue
Block a user