Skip to content

Clippy suggestion gets rid of type inference, causes compiler error #11324

Closed
@villuna

Description

@villuna

Summary

I ran cargo clippy on a parser I wrote in nom, and when it tried to apply a suggestion it ran into an error. Here is the function it ran on:

fn metadata_pair(input: &str) -> IResult<&str, (&str, &str)> {
    separated_pair(
        take_while1(|c| ('A'..='Z').contains(&c)),
        tag(":"),
        opt(is_not("\r\n")).map(|value| value.unwrap_or("")),
    )(input)
}

And here is the output of cargo clippy --fix

The following errors were reported:
error[E0282]: type annotations needed
   --> src/parser/tja_parser.rs:233:22
    |
233 |         take_while1(|c| c.is_ascii_uppercase()),
    |                      ^  - type must be known at this point
    |
help: consider giving this closure parameter an explicit type
    |
233 |         take_while1(|c: /* Type */| c.is_ascii_uppercase()),
    |                       ++++++++++++

...

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0282`.
Original diagnostics will follow.

warning: manual check for common ascii range
   --> src/parser/tja_parser.rs:233:25
    |
233 |         take_while1(|c| ('A'..='Z').contains(&c)),
    |                         ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `c.is_ascii_uppercase()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_is_ascii_check
    = note: `#[warn(clippy::manual_is_ascii_check)]` on by default

I imagine it can't infer the type since the type signature for take_while1 has a lot of indirection:

pub fn take_while1<F, Input, Error: ParseError<Input>>(
  cond: F,
) -> impl Fn(Input) -> IResult<Input, Input, Error>
where
  Input: InputTakeAtPosition,
  F: Fn(<Input as InputTakeAtPosition>::Item) -> bool,

(Though, this is a little strange as I would assume Input should be inferred to be &str, and &str's associated Item type for the InputTakeAtPosition trait is char, but I'm not so familiar with how rustc deals with these kind of type shenanigans).

This is probably a niche bug and it's not that big of a deal, but clippy told me to open an issue so here we are :)

Reproducer

I tried this code:

fn metadata_pair(input: &str) -> IResult<&str, (&str, &str)> {
    separated_pair(
        take_while1(|c| ('A'..='Z').contains(&c)),
        tag(":"),
        opt(is_not("\r\n")).map(|value| value.unwrap_or("")),
    )(input)
}

I ran cargo clippy --fix and the suggestion it applied caused the above error.

Version

rustc 1.71.1 (eb26296b5 2023-08-03)
binary: rustc
commit-hash: eb26296b556cef10fb713a38f3d16b9886080f26
commit-date: 2023-08-03
host: x86_64-unknown-linux-gnu
release: 1.71.1
LLVM version: 16.0.5

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions