Skip to content

Commit 60e2af0

Browse files
committed
also point to value in "value assigned is never read" lint
Thanks to Yemi Bedu for suggesting that we put a span on both sides of the assignment. (Some might argue that we should just put the single span on the right-hand side, but as the change to liveness-dead.rs highlights, it's also nice to have a span on the left when the left-hand side has multiple patterns being assigned.) Resolves #49171.
1 parent 579004f commit 60e2af0

File tree

5 files changed

+34
-30
lines changed

5 files changed

+34
-30
lines changed

src/librustc/middle/liveness.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ use std::io;
117117
use std::rc::Rc;
118118
use syntax::ast::{self, NodeId};
119119
use syntax::symbol::keywords;
120-
use syntax_pos::Span;
120+
use syntax_pos::{Span, MultiSpan};
121121

122122
use hir::Expr;
123123
use hir;
@@ -1363,8 +1363,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Liveness<'a, 'tcx> {
13631363

13641364
fn check_local<'a, 'tcx>(this: &mut Liveness<'a, 'tcx>, local: &'tcx hir::Local) {
13651365
match local.init {
1366-
Some(_) => {
1367-
this.warn_about_unused_or_dead_vars_in_pat(&local.pat);
1366+
Some(ref init_expr) => {
1367+
this.warn_about_unused_or_dead_vars_in_pat(&local.pat, init_expr.span);
13681368
},
13691369
None => {
13701370
this.pat_bindings(&local.pat, |this, ln, var, sp, id| {
@@ -1388,19 +1388,21 @@ fn check_arm<'a, 'tcx>(this: &mut Liveness<'a, 'tcx>, arm: &'tcx hir::Arm) {
13881388

13891389
fn check_expr<'a, 'tcx>(this: &mut Liveness<'a, 'tcx>, expr: &'tcx Expr) {
13901390
match expr.node {
1391-
hir::ExprAssign(ref l, _) => {
1392-
this.check_place(&l);
1391+
hir::ExprAssign(ref l, ref r) => {
1392+
let msp = MultiSpan::from_spans(vec![l.span, r.span]);
1393+
this.check_place(&l, msp);
13931394

1394-
intravisit::walk_expr(this, expr);
1395-
}
1396-
1397-
hir::ExprAssignOp(_, ref l, _) => {
1398-
if !this.tables.is_method_call(expr) {
1399-
this.check_place(&l);
1395+
intravisit::walk_expr(this, expr);
14001396
}
14011397

1402-
intravisit::walk_expr(this, expr);
1403-
}
1398+
hir::ExprAssignOp(_, ref l, ref r) => {
1399+
if !this.tables.is_method_call(expr) {
1400+
let msp = MultiSpan::from_spans(vec![l.span, r.span]);
1401+
this.check_place(&l, msp);
1402+
}
1403+
1404+
intravisit::walk_expr(this, expr);
1405+
}
14041406

14051407
hir::ExprInlineAsm(ref ia, ref outputs, ref inputs) => {
14061408
for input in inputs {
@@ -1410,7 +1412,7 @@ fn check_expr<'a, 'tcx>(this: &mut Liveness<'a, 'tcx>, expr: &'tcx Expr) {
14101412
// Output operands must be places
14111413
for (o, output) in ia.outputs.iter().zip(outputs) {
14121414
if !o.is_indirect {
1413-
this.check_place(output);
1415+
this.check_place(output, MultiSpan::from_span(output.span));
14141416
}
14151417
this.visit_expr(output);
14161418
}
@@ -1435,7 +1437,7 @@ fn check_expr<'a, 'tcx>(this: &mut Liveness<'a, 'tcx>, expr: &'tcx Expr) {
14351437
}
14361438

14371439
impl<'a, 'tcx> Liveness<'a, 'tcx> {
1438-
fn check_place(&mut self, expr: &'tcx Expr) {
1440+
fn check_place(&mut self, expr: &'tcx Expr, msp: MultiSpan) {
14391441
match expr.node {
14401442
hir::ExprPath(hir::QPath::Resolved(_, ref path)) => {
14411443
if let Def::Local(nid) = path.def {
@@ -1445,7 +1447,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14451447
// as being used.
14461448
let ln = self.live_node(expr.id, expr.span);
14471449
let var = self.variable(nid, expr.span);
1448-
self.warn_about_dead_assign(expr.span, expr.id, ln, var);
1450+
self.warn_about_dead_assign(msp, expr.id, ln, var);
14491451
}
14501452
}
14511453
_ => {
@@ -1482,10 +1484,11 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14821484
}
14831485
}
14841486

1485-
fn warn_about_unused_or_dead_vars_in_pat(&mut self, pat: &hir::Pat) {
1487+
fn warn_about_unused_or_dead_vars_in_pat(&mut self, pat: &hir::Pat, assign_span: Span) {
14861488
self.pat_bindings(pat, |this, ln, var, sp, id| {
14871489
if !this.warn_about_unused(sp, id, ln, var) {
1488-
this.warn_about_dead_assign(sp, id, ln, var);
1490+
let msp = MultiSpan::from_spans(vec![sp, assign_span]);
1491+
this.warn_about_dead_assign(msp, id, ln, var);
14891492
}
14901493
})
14911494
}
@@ -1538,16 +1541,17 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15381541
}
15391542

15401543
fn warn_about_dead_assign(&self,
1541-
sp: Span,
1544+
msp: MultiSpan,
15421545
id: NodeId,
15431546
ln: LiveNode,
15441547
var: Variable) {
15451548
if self.live_on_exit(ln, var).is_none() {
1546-
self.report_dead_assign(id, sp, var, false);
1549+
self.report_dead_assign(id, msp, var, false);
15471550
}
15481551
}
15491552

1550-
fn report_dead_assign(&self, id: NodeId, sp: Span, var: Variable, is_argument: bool) {
1553+
fn report_dead_assign<S: Into<MultiSpan>>(&self, id: NodeId, sp: S,
1554+
var: Variable, is_argument: bool) {
15511555
if let Some(name) = self.should_warn(var) {
15521556
if is_argument {
15531557
self.ir.tcx.lint_node(lint::builtin::UNUSED_ASSIGNMENTS, id, sp,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ warning: value assigned to `hours_are_suns` is never read
2929
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:32:9
3030
|
3131
LL | hours_are_suns = false;
32-
| ^^^^^^^^^^^^^^
32+
| ^^^^^^^^^^^^^^ ^^^^^
3333
|
3434
note: lint level defined here
3535
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:13:9

src/test/ui/lint/liveness-dead.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn f1(x: &mut isize) {
1818
}
1919

2020
fn f2() {
21-
let mut x: isize = 3; //~ WARN: value assigned to `x` is never read
21+
let (mut x, y) = (3, 4); //~ WARN: value assigned to `x` is never read
2222
x = 4;
2323
x.clone();
2424
}

src/test/ui/lint/liveness-dead.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: value assigned to `x` is never read
2-
--> $DIR/liveness-dead.rs:21:9
2+
--> $DIR/liveness-dead.rs:21:10
33
|
4-
LL | let mut x: isize = 3; //~ WARN: value assigned to `x` is never read
5-
| ^^^^^
4+
LL | let (mut x, y) = (3, 4); //~ WARN: value assigned to `x` is never read
5+
| ^^^^^ ^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/liveness-dead.rs:14:9
@@ -14,7 +14,7 @@ warning: value assigned to `x` is never read
1414
--> $DIR/liveness-dead.rs:29:5
1515
|
1616
LL | x = 4; //~ WARN: value assigned to `x` is never read
17-
| ^
17+
| ^ ^
1818

1919
warning: value passed to `x` is never read
2020
--> $DIR/liveness-dead.rs:32:7
@@ -26,5 +26,5 @@ warning: value assigned to `x` is never read
2626
--> $DIR/liveness-dead.rs:39:5
2727
|
2828
LL | x = 4; //~ WARN: value assigned to `x` is never read
29-
| ^
29+
| ^ ^
3030

src/test/ui/lint/liveness-unused.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ warning: value assigned to `x` is never read
5252
--> $DIR/liveness-unused.rs:42:5
5353
|
5454
LL | x += 4;
55-
| ^
55+
| ^ ^
5656
|
5757
note: lint level defined here
5858
--> $DIR/liveness-unused.rs:13:27
@@ -104,5 +104,5 @@ warning: value assigned to `x` is never read
104104
--> $DIR/liveness-unused.rs:126:9
105105
|
106106
LL | x = 0; //~ WARN value assigned to `x` is never read
107-
| ^
107+
| ^ ^
108108

0 commit comments

Comments
 (0)