Skip to content

Needless bit mask when casting from a larger integer to a smaller integer #16125

@sorairolake

Description

@sorairolake

What it does

Checks for needless bit mask when casting from a larger integer to a smaller integer to extract the trailing part of the integer. When casting casting from a larger integer to a smaller integer, the excess part is truncated, so bit mask is unnecessary.1

Advantage

Simplify code.

Drawbacks

No response

Example

fn split_half(n: u16) -> (u8, u8) {
    (((n >> 8) & 0xff) as u8, (n & 0xff) as u8)
}

Could be written as:

fn split_half(n: u16) -> (u8, u8) {
    ((n >> 8) as u8, n as u8)
}

Comparison with existing lints

No response

Additional Context

No response

Footnotes

  1. https://doc.rust-lang.org/reference/expressions/operator-expr.html#r-expr.as.numeric.int-truncation

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions