Skip to content

Lint suggesting use of TryFrom for checked integer conversion #3947

Open
@sfackler

Description

@sfackler

Checked integer conversion code has historically looked something like this:

if foo <= i32::max_value() as usize {
    thing_a
} else {
    thing_b
}

But now that TryFrom is stable, it can be done in a more foolproof way like this:

match i32::try_from(foo) {
    Ok(foo) => thing_a,
    Err(_) => thing_b,
}

It'd be cool if there was a lint guiding users to migrate their code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsL-complexityLint: Belongs in the complexity lint groupL-styleLint: Belongs in the style lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions