Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest appropriate code for unused field when destructuring pattern #58883

Merged
merged 1 commit into from
Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,9 +379,22 @@ fn visit_fn<'a, 'tcx: 'a>(ir: &mut IrMaps<'a, 'tcx>,
let body = ir.tcx.hir().body(body_id);

for arg in &body.arguments {
let is_shorthand = match arg.pat.node {
crate::hir::PatKind::Struct(..) => true,
_ => false,
};
arg.pat.each_binding(|_bm, hir_id, _x, ident| {
debug!("adding argument {:?}", hir_id);
fn_maps.add_variable(Arg(hir_id, ident.name));
let var = if is_shorthand {
Local(LocalInfo {
id: hir_id,
name: ident.name,
is_shorthand: true,
})
} else {
Arg(hir_id, ident.name)
};
fn_maps.add_variable(var);
})
};

Expand Down
20 changes: 20 additions & 0 deletions src/test/ui/suggestions/unused-closure-argument.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![deny(unused_variables)]

struct Point {
x: i32,
y: i32,
}

fn main() {
let points = vec!(Point { x: 1, y: 2 }, Point { x: 3, y: 4 });

let _: i32 = points.iter()
.map(|Point { x, y }| y)
//~^ ERROR unused variable
.sum();

let _: i32 = points.iter()
.map(|x| 4)
//~^ ERROR unused variable
.sum();
}
20 changes: 20 additions & 0 deletions src/test/ui/suggestions/unused-closure-argument.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: unused variable: `x`
--> $DIR/unused-closure-argument.rs:12:23
|
LL | .map(|Point { x, y }| y)
| ^ help: try ignoring the field: `x: _`
|
note: lint level defined here
--> $DIR/unused-closure-argument.rs:1:9
|
LL | #![deny(unused_variables)]
| ^^^^^^^^^^^^^^^^

error: unused variable: `x`
--> $DIR/unused-closure-argument.rs:17:15
|
LL | .map(|x| 4)
| ^ help: consider prefixing with an underscore: `_x`

error: aborting due to 2 previous errors