Skip to content

Commit 4468e3e

Browse files
committed
Auto merge of #50675 - csmoe:var_span, r=oli-obk
Reduce span highlighted code in unused_variables lint Fixes #50472 - [X] reduce var span - [ ] mark applicable Before: ``` mut unused_mut_var ^^^^^^^^^^^^^^^^^^ ``` After: ``` mut unused_mut_var ^^^^^^^^^^^^^^ ```
2 parents 9fae153 + c4e99dd commit 4468e3e

File tree

4 files changed

+74
-17
lines changed

4 files changed

+74
-17
lines changed

src/librustc/hir/pat_util.rs

+9
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ impl hir::Pat {
141141
}
142142
}
143143

144+
pub fn simple_span(&self) -> Option<Span> {
145+
match self.node {
146+
PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ref path1, None) |
147+
PatKind::Binding(hir::BindingAnnotation::Mutable, _, ref path1, None) =>
148+
Some(path1.span),
149+
_ => None,
150+
}
151+
}
152+
144153
/// Return variants that are necessary to exist for the pattern to match.
145154
pub fn necessary_variants(&self) -> Vec<DefId> {
146155
let mut variants = vec![];

src/librustc/middle/liveness.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,11 @@ fn visit_arm<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, arm: &'tcx hir::Arm) {
453453
}
454454
}
455455

456-
pat.each_binding(|bm, p_id, sp, path1| {
456+
pat.each_binding(|bm, p_id, _sp, path1| {
457457
debug!("adding local variable {} from match with bm {:?}",
458458
p_id, bm);
459459
let name = path1.node;
460-
ir.add_live_node_for_node(p_id, VarDefNode(sp));
460+
ir.add_live_node_for_node(p_id, VarDefNode(path1.span));
461461
ir.add_variable(Local(LocalInfo {
462462
id: p_id,
463463
name: name,
@@ -628,10 +628,10 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
628628
fn pat_bindings<F>(&mut self, pat: &hir::Pat, mut f: F) where
629629
F: FnMut(&mut Liveness<'a, 'tcx>, LiveNode, Variable, Span, NodeId),
630630
{
631-
pat.each_binding(|_bm, p_id, sp, _n| {
631+
pat.each_binding(|_bm, p_id, sp, n| {
632632
let ln = self.live_node(p_id, sp);
633-
let var = self.variable(p_id, sp);
634-
f(self, ln, var, sp, p_id);
633+
let var = self.variable(p_id, n.span);
634+
f(self, ln, var, n.span, p_id);
635635
})
636636
}
637637

@@ -1398,7 +1398,8 @@ fn check_local<'a, 'tcx>(this: &mut Liveness<'a, 'tcx>, local: &'tcx hir::Local)
13981398
},
13991399
None => {
14001400
this.pat_bindings(&local.pat, |this, ln, var, sp, id| {
1401-
this.warn_about_unused(sp, id, ln, var);
1401+
let span = local.pat.simple_span().unwrap_or(sp);
1402+
this.warn_about_unused(span, id, ln, var);
14021403
})
14031404
}
14041405
}
@@ -1497,7 +1498,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14971498

14981499
fn warn_about_unused_args(&self, body: &hir::Body, entry_ln: LiveNode) {
14991500
for arg in &body.arguments {
1500-
arg.pat.each_binding(|_bm, p_id, sp, path1| {
1501+
arg.pat.each_binding(|_bm, p_id, _, path1| {
1502+
let sp = path1.span;
15011503
let var = self.variable(p_id, sp);
15021504
// Ignore unused self.
15031505
let name = path1.node;
@@ -1541,6 +1543,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15411543

15421544
let suggest_underscore_msg = format!("consider using `_{}` instead",
15431545
name);
1546+
15441547
if is_assigned {
15451548
self.ir.tcx
15461549
.lint_node_note(lint::builtin::UNUSED_VARIABLES, id, sp,

src/test/ui/lint/issue-47390-unused-variable-in-struct-pattern.rs

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ fn main() {
3535
endless_and_singing: true
3636
};
3737

38+
let mut mut_unused_var = 1;
39+
40+
let (mut var, unused_var) = (1, 2);
41+
3842
if let SoulHistory { corridors_of_light,
3943
mut hours_are_suns,
4044
endless_and_singing: true } = who_from_the_womb_remembered {

src/test/ui/lint/issue-47390-unused-variable-in-struct-pattern.stderr

+51-10
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,40 @@ LL | #![warn(unused)] // UI tests pass `-A unused` (#43896)
1111
| ^^^^^^
1212
= note: #[warn(unused_variables)] implied by #[warn(unused)]
1313

14+
warning: unused variable: `mut_unused_var`
15+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:38:13
16+
|
17+
LL | let mut mut_unused_var = 1;
18+
| ^^^^^^^^^^^^^^ help: consider using `_mut_unused_var` instead
19+
20+
warning: unused variable: `var`
21+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:40:14
22+
|
23+
LL | let (mut var, unused_var) = (1, 2);
24+
| ^^^ help: consider using `_var` instead
25+
26+
warning: unused variable: `unused_var`
27+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:40:19
28+
|
29+
LL | let (mut var, unused_var) = (1, 2);
30+
| ^^^^^^^^^^ help: consider using `_unused_var` instead
31+
1432
warning: unused variable: `corridors_of_light`
15-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:38:26
33+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:42:26
1634
|
1735
LL | if let SoulHistory { corridors_of_light,
1836
| ^^^^^^^^^^^^^^^^^^ help: try ignoring the field: `corridors_of_light: _`
1937

2038
warning: variable `hours_are_suns` is assigned to, but never used
21-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:39:26
39+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:43:30
2240
|
2341
LL | mut hours_are_suns,
24-
| ^^^^^^^^^^^^^^^^^^
42+
| ^^^^^^^^^^^^^^
2543
|
2644
= note: consider using `_hours_are_suns` instead
2745

2846
warning: value assigned to `hours_are_suns` is never read
29-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:41:9
47+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:45:9
3048
|
3149
LL | hours_are_suns = false;
3250
| ^^^^^^^^^^^^^^
@@ -39,38 +57,61 @@ LL | #![warn(unused)] // UI tests pass `-A unused` (#43896)
3957
= note: #[warn(unused_assignments)] implied by #[warn(unused)]
4058

4159
warning: unused variable: `case`
42-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:50:23
60+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:54:23
4361
|
4462
LL | Large::Suit { case } => {}
4563
| ^^^^ help: try ignoring the field: `case: _`
4664

4765
warning: unused variable: `case`
48-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:55:24
66+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:59:24
4967
|
5068
LL | &Large::Suit { case } => {}
5169
| ^^^^ help: try ignoring the field: `case: _`
5270

5371
warning: unused variable: `case`
54-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:60:27
72+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:64:27
5573
|
5674
LL | box Large::Suit { case } => {}
5775
| ^^^^ help: try ignoring the field: `case: _`
5876

5977
warning: unused variable: `case`
60-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:65:24
78+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:69:24
6179
|
6280
LL | (Large::Suit { case },) => {}
6381
| ^^^^ help: try ignoring the field: `case: _`
6482

6583
warning: unused variable: `case`
66-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:70:24
84+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:74:24
6785
|
6886
LL | [Large::Suit { case }] => {}
6987
| ^^^^ help: try ignoring the field: `case: _`
7088

7189
warning: unused variable: `case`
72-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:75:29
90+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:79:29
7391
|
7492
LL | Tuple(Large::Suit { case }, ()) => {}
7593
| ^^^^ help: try ignoring the field: `case: _`
7694

95+
warning: variable does not need to be mutable
96+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:38:9
97+
|
98+
LL | let mut mut_unused_var = 1;
99+
| ----^^^^^^^^^^^^^^
100+
| |
101+
| help: remove this `mut`
102+
|
103+
note: lint level defined here
104+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:15:9
105+
|
106+
LL | #![warn(unused)] // UI tests pass `-A unused` (#43896)
107+
| ^^^^^^
108+
= note: #[warn(unused_mut)] implied by #[warn(unused)]
109+
110+
warning: variable does not need to be mutable
111+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:40:10
112+
|
113+
LL | let (mut var, unused_var) = (1, 2);
114+
| ----^^^
115+
| |
116+
| help: remove this `mut`
117+

0 commit comments

Comments
 (0)