Skip to content

type_complexity should not trigger on types using impl trait #17195

@ronnodas

Description

@ronnodas

Summary

The type_complexity lint suggests extracting the complex type as an alias, but this is not allowed (in stable Rust) when the type involves an impl Trait.

Lint Name

type_complexity

Reproducer

I tried this code:

use std::iter::{Map, Zip};

pub fn main() {
    for [x, y] in array_zip([1, 2, 3], [4, 5, 6]) {
        println!("{x} <-> {y}");
    }
}

pub fn array_zip<I, J>(
    left: I,
    right: J,
) -> Map<Zip<I::IntoIter, J::IntoIter>, impl FnMut((I::Item, I::Item)) -> [I::Item; 2]>
where
    I: IntoIterator,
    J: IntoIterator<Item = I::Item>,
{
    left.into_iter().zip(right).map(<[I::Item; 2]>::from)
}

I saw this happen:

    Checking playground v0.0.1 (/playground)
warning: very complex type used. Consider factoring parts into `type` definitions
  --> src/main.rs:12:6
   |
12 | ) -> Map<Zip<I::IntoIter, J::IntoIter>, impl FnMut((I::Item, I::Item)) -> [I::Item; 2]>
   |      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.96.0/index.html#type_complexity
   = note: `#[warn(clippy::type_complexity)]` on by default

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.63s

I expected to see this happen: No lints triggered.

Version

Playground, running 1.96.0 (stable)

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions