Skip to content

single_match does not apply because of type inference failure #15188

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::single_match

this code:

fn main() {
    let string = String::from("foo");
    match string.to_lowercase().as_ref() {
        "foo" => { }
        _ => { }
    };
}

caused the following diagnostics:

    Checking _snippet_0 v0.1.0 (/tmp/icemaker_global_tempdir.IbV1PR0WiGNQ/icemaker_clippyfix_tempdir.gTGcPMAMNPMx/_snippet_0)
warning: you seem to be trying to use `match` for an equality check. Consider using `if`
 --> src/main.rs:3:5
  |
3 | /     match string.to_lowercase().as_ref() {
4 | |         "foo" => { }
5 | |         _ => { }
6 | |     };
  | |_____^ help: try: `if string.to_lowercase().as_ref() == "foo" { }`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match
  = note: requested on the command line with `--force-warn clippy::single-match`

warning: `_snippet_0` (bin "_snippet_0") generated 1 warning (run `cargo clippy --fix --bin "_snippet_0"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s

However after applying these diagnostics, the resulting code:

fn main() {
    let string = String::from("foo");
    if string.to_lowercase().as_ref() == "foo" { };
}

no longer compiled:

    Checking _snippet_0 v0.1.0 (/tmp/icemaker_global_tempdir.IbV1PR0WiGNQ/icemaker_clippyfix_tempdir.gTGcPMAMNPMx/_snippet_0)
error[E0283]: type annotations needed
 --> src/main.rs:3:30
  |
3 |     if string.to_lowercase().as_ref() == "foo" { };
  |                              ^^^^^^
  |
  = note: multiple `impl`s satisfying `std::string::String: std::convert::AsRef<_>` found in the following crates: `alloc`, `std`:
          - impl std::convert::AsRef<[u8]> for std::string::String;
          - impl std::convert::AsRef<std::ffi::OsStr> for std::string::String;
          - impl std::convert::AsRef<std::path::Path> for std::string::String;
          - impl std::convert::AsRef<str> for std::string::String;
help: try using a fully qualified path to specify the expected types
  |
3 -     if string.to_lowercase().as_ref() == "foo" { };
3 +     if <std::string::String as std::convert::AsRef<T>>::as_ref(&string.to_lowercase()) == "foo" { };
  |

error[E0283]: type annotations needed
 --> src/main.rs:3:30
  |
3 |     if string.to_lowercase().as_ref() == "foo" { };
  |                              ^^^^^^   -- type must be known at this point
  |
  = note: multiple `impl`s satisfying `_: std::cmp::PartialEq<str>` found in the following crates: `core`, `std`:
          - impl std::cmp::PartialEq for str;
          - impl std::cmp::PartialEq<str> for std::ffi::OsStr;
          - impl std::cmp::PartialEq<str> for std::ffi::OsString;
  = note: required for `&_` to implement `std::cmp::PartialEq<&str>`
help: try using a fully qualified path to specify the expected types
  |
3 -     if string.to_lowercase().as_ref() == "foo" { };
3 +     if <std::string::String as std::convert::AsRef<T>>::as_ref(&string.to_lowercase()) == "foo" { };
  |

For more information about this error, try `rustc --explain E0283`.
error: could not compile `_snippet_0` (bin "_snippet_0") due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_snippet_0` (bin "_snippet_0" test) due to 2 previous errors

Version:

rustc 1.90.0-nightly (ed2d75978 2025-06-29)
binary: rustc
commit-hash: ed2d759783dc9de134bbb3f01085b1e6dbf539f3
commit-date: 2025-06-29
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

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