Skip to content

Commit 74560f3

Browse files
committed
include mode in unused binding suggestion span
1 parent 5b96425 commit 74560f3

File tree

4 files changed

+119
-5
lines changed

4 files changed

+119
-5
lines changed

src/librustc/middle/liveness.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -1627,11 +1627,18 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
16271627
);
16281628

16291629
if self.ir.variable_is_shorthand(var) {
1630-
err.multipart_suggestion(
1631-
"try ignoring the field",
1632-
spans.iter().map(|span| (*span, format!("{}: _", name))).collect(),
1633-
Applicability::MachineApplicable
1634-
);
1630+
if let Node::Binding(pat) = self.ir.tcx.hir().get_by_hir_id(hir_id) {
1631+
// Handle `ref` and `ref mut`.
1632+
let spans = spans.iter()
1633+
.map(|_span| (pat.span, format!("{}: _", name)))
1634+
.collect();
1635+
1636+
err.multipart_suggestion(
1637+
"try ignoring the field",
1638+
spans,
1639+
Applicability::MachineApplicable,
1640+
);
1641+
}
16351642
} else {
16361643
err.multipart_suggestion(
16371644
"consider prefixing with an underscore",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// run-rustfix
2+
3+
#![deny(unused)]
4+
5+
pub struct S {
6+
pub f1: i32,
7+
}
8+
9+
pub struct Point {
10+
pub x: i32,
11+
pub y: i32,
12+
}
13+
14+
pub enum E {
15+
Variant { field: String }
16+
}
17+
18+
pub fn foo(arg: &E) {
19+
match arg {
20+
E::Variant { field: _ } => (), //~ ERROR unused variable
21+
}
22+
}
23+
24+
fn main() {
25+
let s = S { f1: 123 };
26+
let S { f1: _ } = s; //~ ERROR unused variable
27+
28+
let points = vec![Point { x: 1, y: 2 }];
29+
let _: i32 = points.iter().map(|Point { x: _, y }| y).sum(); //~ ERROR unused variable
30+
31+
match (Point { x: 1, y: 2 }) {
32+
Point { y, x: _ } => y, //~ ERROR unused variable
33+
};
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// run-rustfix
2+
3+
#![deny(unused)]
4+
5+
pub struct S {
6+
pub f1: i32,
7+
}
8+
9+
pub struct Point {
10+
pub x: i32,
11+
pub y: i32,
12+
}
13+
14+
pub enum E {
15+
Variant { field: String }
16+
}
17+
18+
pub fn foo(arg: &E) {
19+
match arg {
20+
E::Variant { ref field } => (), //~ ERROR unused variable
21+
}
22+
}
23+
24+
fn main() {
25+
let s = S { f1: 123 };
26+
let S { ref f1 } = s; //~ ERROR unused variable
27+
28+
let points = vec![Point { x: 1, y: 2 }];
29+
let _: i32 = points.iter().map(|Point { x, y }| y).sum(); //~ ERROR unused variable
30+
31+
match (Point { x: 1, y: 2 }) {
32+
Point { y, ref mut x } => y, //~ ERROR unused variable
33+
};
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error: unused variable: `field`
2+
--> $DIR/issue-54180-unused-ref-field.rs:20:26
3+
|
4+
LL | E::Variant { ref field } => (),
5+
| ----^^^^^
6+
| |
7+
| help: try ignoring the field: `field: _`
8+
|
9+
note: lint level defined here
10+
--> $DIR/issue-54180-unused-ref-field.rs:3:9
11+
|
12+
LL | #![deny(unused)]
13+
| ^^^^^^
14+
= note: #[deny(unused_variables)] implied by #[deny(unused)]
15+
16+
error: unused variable: `x`
17+
--> $DIR/issue-54180-unused-ref-field.rs:29:45
18+
|
19+
LL | let _: i32 = points.iter().map(|Point { x, y }| y).sum();
20+
| ^ help: try ignoring the field: `x: _`
21+
22+
error: unused variable: `f1`
23+
--> $DIR/issue-54180-unused-ref-field.rs:26:17
24+
|
25+
LL | let S { ref f1 } = s;
26+
| ----^^
27+
| |
28+
| help: try ignoring the field: `f1: _`
29+
30+
error: unused variable: `x`
31+
--> $DIR/issue-54180-unused-ref-field.rs:32:28
32+
|
33+
LL | Point { y, ref mut x } => y,
34+
| --------^
35+
| |
36+
| help: try ignoring the field: `x: _`
37+
38+
error: aborting due to 4 previous errors
39+

0 commit comments

Comments
 (0)