Skip to content

Add a warning for casts with loss of precision #17900

Closed

Description

@bfops

e.g. this code should probably generate a warning:

fn main() {
    let i: u16 = 257;
    println!("{}", i as u8);
}

(Sorry if this has been mentioned elsewhere - I didn't see it when searching for "cast warning")

Activity

  1. japaric commented on Oct 10, 2014

    @japaric
    Contributor

    (For the general case, you could use num::cast which returns None if the operation under/overflows (it does a check at runtime).)

    As for the lint request, I think that's a dupe of #14165

  2. bfops commented on Oct 10, 2014

    @bfops
    ContributorAuthor

    Ah good to know! Thanks!

    I don't think it's quite a dupe because that issue only refers to literals - it should maybe be generalized?

  3. steveklabnik commented on Jan 27, 2015

    @steveklabnik
    Contributor

    closing as dup of #5477

  4. added a commit that references this issue on Aug 29, 2024
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