Skip to content

Misleading/Incomplete errors involving Pattern #90970

Closed
@glandium

Description

@glandium

Given the following code:

pub fn strip_lf(s: &str) -> &str {
    s.strip_suffix(b'\n').unwrap_or(s)
}

The current output is:

error[E0277]: expected a `FnMut<(char,)>` closure, found `u8`
 --> src/lib.rs:2:20
  |
2 |     s.strip_suffix(b'\n').unwrap_or(s)
  |                    ^^^^^ expected an `FnMut<(char,)>` closure, found `u8`
  |
  = help: the trait `FnMut<(char,)>` is not implemented for `u8`
  = note: required because of the requirements on the impl of `Pattern<'_>` for `u8`

Pattern<'a> has implementations for:

  • char
  • &str
  • &String
  • &[char]
  • &&str
  • F where F: FnMut(char) -> bool

While it is true that u8 doesn't match any of the non-generic impls, and that it doesn't have an impl for FnMut<(char,)> -> bool, it's sad that the error message doesn't mention that those other possibilities exist.
(Also, the error message doesn't mention the closure return type)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemD-confusingDiagnostics: Confusing error or lint that should be reworked.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions