rust-lang / rust-lang/rust-clippy

warn transmute about non-exhaustives

Open
#10,290 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

This lint emit warnings in following condition.

let T is before-transmute type, and U is after-transmute type.
if valid range of T does not covered by U, this is UB.
This lint bounds T to integral types or enum with #[repr(u_)] or #[repr(i_)] and its all variants does not have fields, and U to #[repr(u_)] or #[repr(i_)].

TODO: If T is enum with #[repr(C)] and its all variants does not have fields, treat it as #[repr(u<GuessedWidth>)].

More spefically:

T U
u8
#[repr(u8)]  
#[repr(u8)]
u8
#[repr(u8)]  
#[repr(i8)]
i8
#[repr(i8)]  
#[repr(u8)]
i8
#[repr(i8)]  
#[repr(i8)]
u16
#[repr(u16)]
#[repr(u16)]
u16
#[repr(u16)]
#[repr(i16)]
i16
#[repr(i16)]
#[repr(u16)]
i16
#[repr(i16)]
#[repr(i16)]
u32
#[repr(u32)]
#[repr(u32)]
u32
#[repr(u32)]
#[repr(i32)]
i32
#[repr(i32)]
#[repr(u32)]
i32
#[repr(i32)]
#[repr(i32)]
u64
#[repr(u64)]
#[repr(u64)]
u64
#[repr(u64)]
#[repr(i64)]
i64
#[repr(i64)]
#[repr(u64)]
i64
#[repr(i64)]
#[repr(i64)]

If their value may not overwrap (i.e. integer to one-element enum), emits warning.
If their value never overwrap (i.e. Foo { One = 1 } and Bar { Two = 2 }), transmuting between them is 100% UB and emits error (this should be given another lint name, like transmute_non_overwrapping_enums under correctness)
If U is 100% exhaustive over integral possible values (i.e. #[repr(u8)] and having 256 elements), this lint does not fire.

Lint Name

transmute_integer_to_out_of_enum

Category

suspicious

Advantage
  • Avoids some form of UB.
Drawbacks

No response

Example
#[repr(u8)]
enum Foo {
    Zero,
    One,
    Two,
}

let invalid = 3;
let ub: Foo = unsafe { std::mem::transmute(invalid) };
//                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Could be written as:

impl Foo {
    fn new(inner: u8) -> Option<Foo> {
        // match clause
    }
}
let invalid = 3;
let not_ub: Foo = Foo::new(invalid).expect_err("should not succeed")

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue does not name files, tests, or an entry point. Start by reviewing the proposed transmute_integer_to_out_of_enum behavior and the Rust transmute and repr rules described here. Done means the lint handles the listed integer and fieldless repr enum combinations, including exhaustive cases, and reports the specified warnings or errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.