Skip to content

Commit a35ece4

Browse files
authored
Rollup merge of rust-lang#56439 - JohnGinger:master, r=nikomatsakis
Clearer error message for dead assign I'm not that this is the right place for this (if it needs an RFC or not). I had the problem where I misunderstood the compiler lint message rust-lang#56436 and other people seem to have had the same problem https://www.reddit.com/r/rust/comments/8cy9p4/value_assigned_to_is_never_read/. I think this new wording might be slightly clearer (and help out beginners like me). I'm very new though, so there might be some nuance I'm missing that would make this more confusing or a bad idea for other reasons. I thought I would create a PR to make it easy to change the code if the consensus was that it would make sense to make a change. If this is the wrong place for this sort of thing I'll happily delete/move it.
2 parents af755f8 + c0e3f4b commit a35ece4

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

src/librustc/middle/liveness.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,11 +1657,15 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
16571657
fn report_dead_assign(&self, hir_id: HirId, sp: Span, var: Variable, is_argument: bool) {
16581658
if let Some(name) = self.should_warn(var) {
16591659
if is_argument {
1660-
self.ir.tcx.lint_hir(lint::builtin::UNUSED_ASSIGNMENTS, hir_id, sp,
1661-
&format!("value passed to `{}` is never read", name));
1660+
self.ir.tcx.struct_span_lint_hir(lint::builtin::UNUSED_ASSIGNMENTS, hir_id, sp,
1661+
&format!("value passed to `{}` is never read", name))
1662+
.help("maybe it is overwritten before being read?")
1663+
.emit();
16621664
} else {
1663-
self.ir.tcx.lint_hir(lint::builtin::UNUSED_ASSIGNMENTS, hir_id, sp,
1664-
&format!("value assigned to `{}` is never read", name));
1665+
self.ir.tcx.struct_span_lint_hir(lint::builtin::UNUSED_ASSIGNMENTS, hir_id, sp,
1666+
&format!("value assigned to `{}` is never read", name))
1667+
.help("maybe it is overwritten before being read?")
1668+
.emit();
16651669
}
16661670
}
16671671
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ note: lint level defined here
5555
LL | #![warn(unused)] // UI tests pass `-A unused` (#43896)
5656
| ^^^^^^
5757
= note: #[warn(unused_assignments)] implied by #[warn(unused)]
58+
= help: maybe it is overwritten before being read?
5859

5960
warning: unused variable: `fire`
6061
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:54:32

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,31 @@ note: lint level defined here
99
|
1010
LL | #![deny(unused_assignments)]
1111
| ^^^^^^^^^^^^^^^^^^
12+
= help: maybe it is overwritten before being read?
1213

1314
error: value assigned to `x` is never read
1415
--> $DIR/liveness-dead.rs:27:5
1516
|
1617
LL | x = 4; //~ ERROR: value assigned to `x` is never read
1718
| ^
19+
|
20+
= help: maybe it is overwritten before being read?
1821

1922
error: value passed to `x` is never read
2023
--> $DIR/liveness-dead.rs:30:11
2124
|
2225
LL | fn f4(mut x: i32) { //~ ERROR: value passed to `x` is never read
2326
| ^
27+
|
28+
= help: maybe it is overwritten before being read?
2429

2530
error: value assigned to `x` is never read
2631
--> $DIR/liveness-dead.rs:37:5
2732
|
2833
LL | x = 4; //~ ERROR: value assigned to `x` is never read
2934
| ^
35+
|
36+
= help: maybe it is overwritten before being read?
3037

3138
error: aborting due to 4 previous errors
3239

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ note: lint level defined here
6060
|
6161
LL | #![deny(unused_assignments)]
6262
| ^^^^^^^^^^^^^^^^^^
63+
= help: maybe it is overwritten before being read?
6364

6465
error: variable `z` is assigned to, but never used
6566
--> $DIR/liveness-unused.rs:47:13
@@ -106,6 +107,8 @@ error: value assigned to `x` is never read
106107
|
107108
LL | x = 0; //~ ERROR value assigned to `x` is never read
108109
| ^
110+
|
111+
= help: maybe it is overwritten before being read?
109112

110113
error: aborting due to 13 previous errors
111114

0 commit comments

Comments
 (0)