False positive derivable_impls
for type parameters with defaults
#10396
Labels
C-bug
Category: Clippy is not doing the correct thing
I-false-positive
Issue: The lint was triggered on code it shouldn't have
I-suggestion-causes-error
Issue: The suggestions provided by this Lint cause an ICE/error when applied
Uh oh!
There was an error while loading. Please reload this page.
Summary
In certain cases involving type parameters with defaults, it is possible for clippy to suggest re-writing an
impl
block using aderive
, when in fact aderive
would be more generic and may even cause a compiler error.Lint Name
derivable_impls
Reproducer
I tried this code:
This triggers the lint saying the
impl
can instead be derived:This is a false positive as a
derive
here would be semantically different. Theimpl
block as written implementsDefault
specifically forGenericType
, which is equivalent toGenericType<DefaultType>
. However aderive
would implementDefault
for anyGenericType<T: Default>
.A similar case is the following, with an additional layer of indirection:
This triggers the lint:
But following the advice would create a compiler error:
The difference here is that the
impl
block implementsDefault
forGenericType<DefaultType>
, whereas thederive
implementsDefault
forGenericType<T: Default>
. Thederive
therefore has the trait bound thatInnerGenericType<T>
implementsDefault
for allT: Default
, which isn't satisfied: onlyInnerGenericType<DefaultType>
implementsDefault
.(And breathe.)
This is similar to #7655 which was fixed – this one still occurs likely because of the special case with the type parameter having a default (
<T = DefaultType>
). The lint isn't triggered if the type parameter is explicitly specified, i.e.:instead of
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: