Skip to content

Suggest cast_signed and cast_unsigned instead of as #14974

Open
@ojeda

Description

@ojeda

What it does

cast_signed and cast_unsigned are stable since 1.87.

They produce the same result as an as cast, but they ensure that the bit-width remains the same.

Thus, for projects that may want to avoid as casts as much as possible, using cast_{un,}signed may be positive.

Advantage

Reduce the need for as casts.

Drawbacks

No response

Example

u32::MAX as i32
-1i32 as u32

Could be written as:

u32::MAX.cast_signed()
(-1i32).cast_unsigned()

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