rust-lang / rust-lang/rust-clippy
`type_complexity` fires on type that cannot be simplified
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
I have a type that is admittedly complicated, but due to its structure,
I believe that there is no way to factor it apart.
Lint Name
type_complexity
Reproducer
I tried this code:
pub struct Filters<'f, E> {
pub widgets: Box<dyn for<'a> FnOnce(&'a Widget) -> Result<(), E> + 'f>,
pub gadgets: Box<dyn for<'a> FnOnce(Gadget<'a>) -> Result<(), E> + 'f>,
}
pub struct Widget;
pub struct Gadget<'a>(&'a ());
I saw this happen:
warning: very complex type used. Consider factoring parts into `type` definitions
--> src/lib.rs:2:18
|
2 | pub widgets: Box<dyn for<'a> FnOnce(&'a Widget) -> Result<(), E> + 'f>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity
= note: `#[warn(clippy::type_complexity)]` on by default
warning: very complex type used. Consider factoring parts into `type` definitions
--> src/lib.rs:3:18
|
3 | pub gadgets: Box<dyn for<'a> FnOnce(Gadget<'a>) -> Result<(), E> + 'f>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity
warning: `playground` (lib) generated 2 warnings
See playground link.
I expected to see this happen:
No warning, since the type cannot be simplified.
I would love to simplify these types. Ideally, I would write something like:
// does not compile!
pub type Filter<'f, E, T> = Box<dyn for<'a> FnOnce(T<'a>) -> Result<(), E> + 'f>;
pub struct Filters<'f, E> {
pub widgets: Filter<'f, E, &_ Widget>, // made-up syntax!
pub gadgets: Filter<'f, E, Gadget<_>>, // made-up syntax!
}
pub struct Widget;
pub struct Gadget<'a>(&'a ());
But of course this does not work. I don't see a way to break down any
meaningful part of this type expression. (Even breaking out the type parameter
to Box<_> would require trait aliases, which aren't available on stable.)
Clippy shouldn't prompt me to improve code along an axis that admits no further
improvements.
Version
rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: x86_64-unknown-linux-gnu
release: 1.70.0
LLVM version: 16.0.2
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the type_complexity lint entry point and reproduce the report with the Rust snippet in the issue. Add a regression case covering the two higher-ranked FnOnce fields; the work is done when the lint no longer recommends factoring types that cannot be meaningfully simplified.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100