Skip to content
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

warning: value assigned to x is never read not fixed by cargo fix #76847

Open
jrmuizel opened this issue Sep 17, 2020 · 4 comments
Open

warning: value assigned to x is never read not fixed by cargo fix #76847

jrmuizel opened this issue Sep 17, 2020 · 4 comments
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jrmuizel
Copy link
Contributor

I tried this code:

fn main() {
    let mut x = 0;
    x = 8;
    println!("Hello, world! {}", x);
}

which gives:

warning: value assigned to `x` is never read
 --> src/main.rs:2:13
  |
2 |     let mut x = 0;
  |             ^
  |
  = note: `#[warn(unused_assignments)]` on by default
  = help: maybe it is overwritten before being read?

It would be nice if this warning was fixable with cargo fix

@jrmuizel jrmuizel added the C-bug Category: This is a bug. label Sep 17, 2020
@newca12
Copy link

newca12 commented Sep 17, 2020

https://doc.rust-lang.org/cargo/commands/cargo-fix.html
If you encounter any problems with cargo fix or otherwise have any questions or feature requests please don’t hesitate to file an issue at https://github.com/rust-lang/cargo

@jrmuizel
Copy link
Contributor Author

I filed this here because I don't think the problem is in cargo fix. I think rustc needs to suggest a fix and then cargo fix will just work.

@jyn514 jyn514 added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 17, 2020
@jyn514
Copy link
Member

jyn514 commented Sep 17, 2020

Relevant code:

fn report_unsed_assign(
&self,
hir_id: HirId,
spans: Vec<Span>,
var: Variable,
message: impl Fn(&str) -> String,
) {
if let Some(name) = self.should_warn(var) {
self.ir.tcx.struct_span_lint_hir(
lint::builtin::UNUSED_ASSIGNMENTS,
hir_id,
spans,
|lint| {
lint.build(&message(&name))
.help("maybe it is overwritten before being read?")
.emit();
},
)
}
}

Seems like the hard part will be coming up with a suggestion, maybe just to remove the assignment?

@jyn514 jyn514 added C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed C-bug Category: This is a bug. labels Sep 17, 2020
@lcnr
Copy link
Contributor

lcnr commented Sep 18, 2020

We can't just remove the assignment here, see

fn weird<T>(v: T) -> T {
    println!("hey");
    v
}

fn main() {
    let mut x = weird(0);
    x = 8;
}

We could maybe recommend

let mut x; wierd(0); 

here, which should result in followup warnings, not sure if that's too great though

Or we only add the suggestion if the input is known to not have sideeffects, which is probably better 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants