Skip to content

useless_conversion is triggered in generated code #17083

Description

@nazar-pc

Summary

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=5d0bac9a5c89f0417bc3b2212531f609

The issue here is that the code is generated. build.rs only really knows that RegType can be constructed from u32, but not that the particular implementation is in fact using u32 specifically. Generics and function body are assembled from different places and parsing generics and handling type aliases is not really feasible in a general case.

It'd be very convenient if this specific case of ::from() that involves generics didn't trigger useless_conversion in generated code specifically. I'm not so sure about more general case though.

Lint Name

useless_conversion

Reproducer

I tried this code:

#![warn(clippy::useless_conversion)]

pub trait RegType
where
    Self: From<u32>,
{
}

pub trait Register {
    type Type: RegType;
}

pub trait Instruction {
    type Reg: Register;
}

pub trait WriteT
where
    Self: Instruction,
{
    fn write(output: &mut <<Self as Instruction>::Reg as Register>::Type);
}

pub enum TestInstruction<Reg> {
    Add(Reg),
}

impl<Reg> Instruction for TestInstruction<Reg>
where
    Reg: Register,
{
    type Reg = Reg;
}

#[automatically_derived]
impl<Reg> WriteT for TestInstruction<Reg>
where
    Reg: Register<Type = u32>,
{
    fn write(output: &mut Reg::Type) {
        *output = !Reg::Type::from(1u32);
    }
}

I saw this happen:

warning: useless conversion to the same type: `u32`
  --> src/lib.rs:41:20
   |
41 |         *output = !Reg::Type::from(1u32);
   |                    ^^^^^^^^^^^^^^^^^^^^^ help: consider removing `Reg::Type::from()`: `1u32`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.95.0/index.html#useless_conversion
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
 1 | #![warn(clippy::useless_conversion)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^

I expected to see this happen: No warnings

Version


Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions