Properly account for non-shorthand pattern field in unused variable lint
Fix #82488
This commit is contained in:
parent
a8486b64b0
commit
fb24a10ad3
4 changed files with 56 additions and 5 deletions
|
@ -367,12 +367,17 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
|
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
|
||||||
let is_shorthand = matches!(param.pat.kind, rustc_hir::PatKind::Struct(..));
|
|
||||||
param.pat.each_binding(|_bm, hir_id, _x, ident| {
|
param.pat.each_binding(|_bm, hir_id, _x, ident| {
|
||||||
let var = if is_shorthand {
|
let var = match param.pat.kind {
|
||||||
Local(LocalInfo { id: hir_id, name: ident.name, is_shorthand: true })
|
rustc_hir::PatKind::Struct(_, fields, _) => Local(LocalInfo {
|
||||||
} else {
|
id: hir_id,
|
||||||
Param(hir_id, ident.name)
|
name: ident.name,
|
||||||
|
is_shorthand: fields
|
||||||
|
.iter()
|
||||||
|
.find(|f| f.ident == ident)
|
||||||
|
.map_or(false, |f| f.is_shorthand),
|
||||||
|
}),
|
||||||
|
_ => Param(hir_id, ident.name),
|
||||||
};
|
};
|
||||||
self.add_variable(var);
|
self.add_variable(var);
|
||||||
});
|
});
|
||||||
|
|
16
src/test/ui/lint/unused_variables-issue-82488.fixed
Normal file
16
src/test/ui/lint/unused_variables-issue-82488.fixed
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
// run-rustfix
|
||||||
|
#![deny(unused_variables)]
|
||||||
|
|
||||||
|
struct Point {
|
||||||
|
x: u32,
|
||||||
|
y: u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_point(Point { x, y: _renamed }: Point) {
|
||||||
|
//~^ ERROR unused variable: `renamed`
|
||||||
|
let _ = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
process_point(Point { x: 0, y: 0 });
|
||||||
|
}
|
16
src/test/ui/lint/unused_variables-issue-82488.rs
Normal file
16
src/test/ui/lint/unused_variables-issue-82488.rs
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
// run-rustfix
|
||||||
|
#![deny(unused_variables)]
|
||||||
|
|
||||||
|
struct Point {
|
||||||
|
x: u32,
|
||||||
|
y: u32
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_point(Point { x, y: renamed }: Point) {
|
||||||
|
//~^ ERROR unused variable: `renamed`
|
||||||
|
let _ = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
process_point(Point { x: 0, y: 0 });
|
||||||
|
}
|
14
src/test/ui/lint/unused_variables-issue-82488.stderr
Normal file
14
src/test/ui/lint/unused_variables-issue-82488.stderr
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
error: unused variable: `renamed`
|
||||||
|
--> $DIR/unused_variables-issue-82488.rs:9:32
|
||||||
|
|
|
||||||
|
LL | fn process_point(Point { x, y: renamed }: Point) {
|
||||||
|
| ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_renamed`
|
||||||
|
|
|
||||||
|
note: the lint level is defined here
|
||||||
|
--> $DIR/unused_variables-issue-82488.rs:2:9
|
||||||
|
|
|
||||||
|
LL | #![deny(unused_variables)]
|
||||||
|
| ^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Reference in a new issue