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
Summary
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=5d0bac9a5c89f0417bc3b2212531f609
The issue here is that the code is generated.
build.rsonly really knows thatRegTypecan be constructed fromu32, but not that the particular implementation is in fact usingu32specifically. 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 triggeruseless_conversionin generated code specifically. I'm not so sure about more general case though.Lint Name
useless_conversion
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen: No warnings
Version
Additional Labels
No response