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

Negation of unsigned literals lint does not fire when literal type is deduced #16445

Closed
netvl opened this issue Aug 12, 2014 · 3 comments
Closed
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.

Comments

@netvl
Copy link
Contributor

netvl commented Aug 12, 2014

This leads to somewhat confusing results (see this SO question). Example code:

fn main() {
    let pair = (2953495866u, -2953495866);

    println!("Tell me about {}", pair);
    match pair {
        (x, y) if x == y => println!("These are twins"),
        (x, y) if x + y == 0 => println!("Antimatter, kaboom!"),
        (x, _) if x % 2 == 1 => println!("The first one is odd"),
        _ => println!("No correlation..."),
    }
}

Prints

Tell me about (2953495866, 18446744070756055750)
Antimatter, kaboom!

No compiler output is produced.

However, when u is added to the second literal:

fn main() {
    let pair = (2953495866u, -2953495866u);

    println!("Tell me about {}", pair);
    match pair {
        (x, y) if x == y => println!("These are twins"),
        (x, y) if x + y == 0 => println!("Antimatter, kaboom!"),
        (x, _) if x % 2 == 1 => println!("The first one is odd"),
        _ => println!("No correlation..."),
    }
}

A warning is printed by the compiler:

<anon>:3:30: 3:42 warning: negation of unsigned int literal may be unintentional, #[warn(unsigned_negate)] on by default
<anon>:3     let pair = (2953495866u, -2953495866u);
                                      ^~~~~~~~~~~~
@tomjakubowski
Copy link
Contributor

Related I think: there is no warning when you provide a negative literal when indexing a Vec or something like it which implements Index<uint, _>. This is potentially confusing to those coming from languages where -1 is used to index the last element of a collection.

@kazagistar
Copy link

Just to help provide a minimal test case:

fn main() {
    let x: u32 = -10; -- no warning
    println!("{}", x);
}

Basically, the linter seems to only fire if there is a - and u in the same literal, but does not actually care about negative literals being assigned to and unsigned datatype.

@huonw huonw added the A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. label Jan 8, 2015
@huonw
Copy link
Member

huonw commented Jan 13, 2015

Dupe of #5477.

@huonw huonw closed this as completed Jan 13, 2015
matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this issue Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut.
Projects
None yet
Development

No branches or pull requests

4 participants