rust-lang / rust-lang/rust-clippy

`clippy::type_complexity` has become more sensitive

Open
#9,299 2 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

The following code (an excerpt from my actual code) produces a clippy::type_complexity warning on nightly, but not on stable 1.62.1. I think this should be considered a regression, because the type does not really have very much nesting to it, and would not be made clearer by introducing a type alias — it's “just” a boxed function.

Lint Name

type_complexity

Reproducer
use std::{error::Error, rc::Rc};

type PredicateRes = Result<(), Box<dyn Error>>;
struct TransactionAndPredicate<'a, Tr, Ta> {
    transaction: Tr,
    predicate: Rc<dyn Fn(&Ta, &Ta) -> PredicateRes + 'a>,
}

I saw this happen:

warning: very complex type used. Consider factoring parts into `type` definitions
 --> src/lib.rs:6:16
  |
6 |     predicate: Rc<dyn Fn(&Ta, &Ta) -> PredicateRes + 'a>,
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(clippy::type_complexity)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity

I expected to see this happen: No warning.

Version
rustc 1.64.0-nightly (affe0d3a0 2022-08-05)
binary: rustc
commit-hash: affe0d3a00e92fa7885e3f5d2c5073fde432d154
commit-date: 2022-08-05
host: x86_64-apple-darwin
release: 1.64.0-nightly
LLVM version: 14.0.6
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Rust reproducer with the type_complexity lint enabled, then inspect how the lint handles the shown Rc<dyn Fn...> field. Done means the reproducer no longer emits the warning while the lint remains available for genuinely complex types.

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
Clearly specified
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.