Skip to content

More DRY code for const definitions that contain an as cast #15178

Open
@leonardo-m

Description

@leonardo-m

What it does

It suggests to replace a coding pattern that isn't DRY enough when there's a cast in a const definition. This is a spinoff of issue #15163 .

Advantage

Less types to read, less code to change if needed, code more DRY.

Drawbacks

I can't think of any drawback. But perhaps there are some.

Example

In the wild (Linux) I've seen Rust code like this:

const BMCR_SPEED100: u16 = uapi::BMCR_SPEED100 as u16;

This is similar but simplified code:

#![warn(clippy::all)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]
fn main() {
    const M: usize = 100;
    const N: u16 = M as u16;
}

I think a new Clippy lint could suggest instead:

const N: u16 = M as _;

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