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 for assignment to temporary #4497

Closed
no111u3 opened this issue Sep 3, 2019 · 4 comments
Closed

warning for assignment to temporary #4497

no111u3 opened this issue Sep 3, 2019 · 4 comments

Comments

@no111u3
Copy link

no111u3 commented Sep 3, 2019

For simple useless code:

    fn main() {
        (0, ).0 = 1;
    }

Compiler not generate any warnings/errors, but clippy generate one warning:

warning: assignment to temporary
 --> src/main.rs:4:5
  |
4 |     (0, ).0 = 1;
  |     ^^^^^^^^^^^
  |

I think this warning need to implement in compiler too.

@sanxiyn
Copy link
Member

sanxiyn commented Sep 4, 2019

FYI, this is temporary_assignment lint. rust-lang/rust#53224 is the general issue for upstreaming Clippy lints. (temporary_assignment is not included in that issue, but it probably should be?)

@flip1995
Copy link
Member

flip1995 commented Sep 4, 2019

The rust issue only looks at correctness lints. temporary_assignment is a complexity lint. Doing something like this cannot result in wrong code, since you can't use the structure you temporary assigned a value to (The assignment operator = always returns () in Rust, so a = b = 1, will result in a = () and b = 1*).

So this is valid rust code, that does not have any influence on the correctness of your program. So this doesn't qualify for a rustc lint, because for such cases Clippy exists.


* This seems that it may cause errors, but if you would try to use a = () as an integer somewhere in your program, this would be caught by the rust type system, so we're good here.

@flip1995 flip1995 closed this as completed Sep 4, 2019
@sanxiyn
Copy link
Member

sanxiyn commented Sep 4, 2019

I very much disagree in that Rust's standard unused_variables lint is not a correctness issue either. It does make sense to prioritize correctness lints though.

@flip1995
Copy link
Member

flip1995 commented Sep 4, 2019

I agree, that rust has lints, that aren't really correctness concerns (most of them are allow-by-default though). But on the other hand, the rust compiler still has a pretty strict "no new lints" rule, except, there are really good reasons to add a new lint. That's why Clippy exists.


As a site note: unused_variables can lead to errors:

let similar_name1 = 10;
let similar_nameI = 11; // unused_variable

assert_eq!(similar_name1, 11); // panic

That is a very constructed example, but it does happen in actual code. (BTW, Clippy has a lint for this: https://rust-lang.github.io/rust-clippy/master/index.html#similar_names)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants