Skip to content

Conflict between pattern_type_mismatch and ref_patterns #14690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hashcatHitman opened this issue Apr 25, 2025 · 2 comments
Closed

Conflict between pattern_type_mismatch and ref_patterns #14690

hashcatHitman opened this issue Apr 25, 2025 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@hashcatHitman
Copy link

Summary

With the pattern_type_mismatch and ref_patterns lints both enabled, you can get stuck in a situation where you are told:

type of pattern does not match the expression type
use `*` to dereference the match expression or explicitly match against a `&_` pattern and adjust the enclosed variable bindings

If you're using the reproduction code given below, this both options will initially lead to:
cannot move `String` out of reference

You can prefix the internal variables with ref to fix this, which leads to:

usage of ref pattern
consider using `&` for clarity instead

Reproducer

I tried this code:

enum Foo {
    Bar(String),
    Baz(String),
}

impl Foo {
    fn qux(&self) {
        match *self {
            Foo::Bar(ref quux) => println!("{quux}"),
            Foo::Baz(ref quuux) => println!("{quuux}"),
        }
    }
}

I expected to see this happen:
No warning, or a config to allow it in this context.

Instead, this happened:

usage of ref pattern
consider using `&` for clarity instead

When using & is not valid in this context.

Version

rustc 1.88.0-nightly (df35ff6c3 2025-04-23)
binary: rustc
commit-hash: df35ff6c354f1f1fbf430b84e7dea37dfe997f34
commit-date: 2025-04-23
host: x86_64-pc-windows-msvc
release: 1.88.0-nightly
LLVM version: 20.1.2

Additional Labels

No response

@hashcatHitman hashcatHitman added the C-bug Category: Clippy is not doing the correct thing label Apr 25, 2025
@Jarcho
Copy link
Contributor

Jarcho commented Apr 25, 2025

Both lints are working as expected. What they check for directly contradict each other, but that's acceptable for lints in the restriction category.

From the readme:

The restriction category should, emphatically, not be enabled as a whole. The contained lints may lint against perfectly reasonable code, may not have an alternative suggestion, and may contradict any other lints (including other categories). Lints should be considered on a case-by-case basis before enabling.

@Jarcho Jarcho closed this as completed Apr 25, 2025
@hashcatHitman
Copy link
Author

I do understand that the restriction category may conflict itself and should not be enabled as a whole. The issue is I forgot how ref works - after checking the reference, I understand now why they're incompatible. For some reason I was under the impression ref was just a slightly different way of saying &. Sorry about that, thanks for pointing out that they contradict each other!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

No branches or pull requests

2 participants