Skip to content

warning for assignment to temporary #4497

Closed

Description

@no111u3

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.

Activity

  1. sanxiyn commented on Sep 4, 2019

    @sanxiyn
    Contributor

    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?)

  2. flip1995 commented on Sep 4, 2019

    @flip1995
    Member

    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.

  3. sanxiyn commented on Sep 4, 2019

    @sanxiyn
    Contributor

    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.

  4. flip1995 commented on Sep 4, 2019

    @flip1995
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions