Skip to content

Clearer error message for dead assign #56439

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

Merged
merged 5 commits into from
Dec 16, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 4 additions & 2 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,10 +1658,12 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
if let Some(name) = self.should_warn(var) {
if is_argument {
self.ir.tcx.lint_hir(lint::builtin::UNUSED_ASSIGNMENTS, hir_id, sp,
&format!("value passed to `{}` is never read", name));
&format!("value passed to `{}` is never read
(maybe it is overwritten before being read)", name));
} else {
self.ir.tcx.lint_hir(lint::builtin::UNUSED_ASSIGNMENTS, hir_id, sp,
&format!("value assigned to `{}` is never read", name));
&format!("value assigned to `{}` is never read
(maybe it is overwritten before being read)", name));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it's a nice idea to make a suggestion like this. It'd be nicer if we could find the overwriting assignment. But i'm reluctant to suggest trying to implement that, since this liveness code is ripe to be rewritten completely.

Still, I think that including the suggestion in a parenthetical makes it less likely to be noticed, and more likely to wrap around on the terminal.

A middle option would be to create a "help". You could change this code from self.ir.tcx.lint_hir(..) to:

let msg = self.ir.tcx.self.struct_span_lint_hir(...)
  .help("maybe it is overwritten before being read?")
  .emit();

and I think things would look better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

I could try rewriting the liveness code, but I don't think I know enough about how it works...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to talk you through it =) but it'd obviously be a rather bigger endeavor :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to try :)

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ LL | mut hours_are_suns,
= note: consider using `_hours_are_suns` instead

warning: value assigned to `hours_are_suns` is never read
(maybe it is overwritten before being read)
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:50:9
|
LL | hours_are_suns = false;
Expand Down
4 changes: 4 additions & 0 deletions src/test/ui/liveness/liveness-dead.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
error: value assigned to `x` is never read
(maybe it is overwritten before being read)
--> $DIR/liveness-dead.rs:19:13
|
LL | let mut x: isize = 3; //~ ERROR: value assigned to `x` is never read
Expand All @@ -11,18 +12,21 @@ LL | #![deny(unused_assignments)]
| ^^^^^^^^^^^^^^^^^^

error: value assigned to `x` is never read
(maybe it is overwritten before being read)
--> $DIR/liveness-dead.rs:27:5
|
LL | x = 4; //~ ERROR: value assigned to `x` is never read
| ^

error: value passed to `x` is never read
(maybe it is overwritten before being read)
--> $DIR/liveness-dead.rs:30:11
|
LL | fn f4(mut x: i32) { //~ ERROR: value passed to `x` is never read
| ^

error: value assigned to `x` is never read
(maybe it is overwritten before being read)
--> $DIR/liveness-dead.rs:37:5
|
LL | x = 4; //~ ERROR: value assigned to `x` is never read
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/liveness/liveness-unused.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ LL | let mut x = 3;
= note: consider using `_x` instead

error: value assigned to `x` is never read
(maybe it is overwritten before being read)
--> $DIR/liveness-unused.rs:42:5
|
LL | x += 4;
Expand Down Expand Up @@ -102,6 +103,7 @@ LL | let x;
= note: consider using `_x` instead

error: value assigned to `x` is never read
(maybe it is overwritten before being read)
--> $DIR/liveness-unused.rs:126:9
|
LL | x = 0; //~ ERROR value assigned to `x` is never read
Expand Down