You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Warn if type parameters are named identically but ordered inconsistently in type definitions and impl-blocks. This is a cousin to the new inconsistent_struct_constructor lint. With literal constructors, the order of variables does not matter (Foo { a, b } is the same as Foo { b, a }). Type parameters, on the other hand, are never referred to by name, but only by position, which is exactly reversed to the behavior of struct constructors. For example:
structFoo<A,B>{a:A,b:B,}// Potential warning: `B` does not refer to `B` in the struct-definition, because of position.impl<B,A>Foo<B,A>{fntype_names(&self){let name_a = std::any::type_name::<A>();let name_b = std::any::type_name::<B>();println!("A: {}, B: {}", name_a, name_b);}fnvalues(&self)whereA: std::fmt::Debug,B: std::fmt::Debug,{println!("a: {:?}, b: {:?}",self.a,self.b);}}fnmain(){let foo = Foo{a:0,b:"foo"};// Prints 'A: &str, B: i32'
foo.type_names();// Prints 'a: 0, b: "foo"'
foo.values();// Wait, what? A `i32` valued as "foo" ?}
Categories (optional)
Kind: Possibly clippy::correctness, because getting this wrong may cause you to refer to the wrong type parameter.
What is the advantage of the recommended code over the original code
Naming the type parameters consistently avoids confusion between type definition and type implementation.
Drawbacks
None.
Example
// Note::structFoo<A,B>{// ...
impl<B,A>Foo<B,A>{// ^--|- This type parameter refers to `Foo::A`, but is named `B`.// ^- This type parameter refers to `Foo::B`, but is named `A`
Could be written as:
impl<A,B>Foo<A,B>{
The text was updated successfully, but these errors were encountered:
Also consider cases like impl<T, A> Foo<T, A> where just the A is out of place. Maybe name the lint inconsistent_type_param_position.
We could also make a stricter lint which fires any time the variable name does not match. Then Foo<C, D> would lint because it expects <A, B>. type_param_mismatch
Add new lint `mismatching_type_param_order`
changelog:
Add new lint `mismatching_type_param_order` for checking if type parameters
are consistent between type definitions and impl blocks.
fixes#7147
Uh oh!
There was an error while loading. Please reload this page.
What it does
Warn if type parameters are named identically but ordered inconsistently in type definitions and impl-blocks. This is a cousin to the new
inconsistent_struct_constructor
lint. With literal constructors, the order of variables does not matter (Foo { a, b }
is the same asFoo { b, a }
). Type parameters, on the other hand, are never referred to by name, but only by position, which is exactly reversed to the behavior of struct constructors. For example:Categories (optional)
clippy::correctness
, because getting this wrong may cause you to refer to the wrong type parameter.What is the advantage of the recommended code over the original code
Drawbacks
None.
Example
Could be written as:
The text was updated successfully, but these errors were encountered: