Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::pattern_type_mismatch: type of pattern does not match the expression type
What it meansChecks for patterns that aren’t exact representations of the types they are applied to. To satisfy this lint, you will have to adjust either the expression that is matched against or the pattern itself, as well as the bindings that are introduced by the adjusted patterns. For matching you will have to either dereference the expression with the * operator, or amend the patterns to explicitly match against &<pattern> or &mut <pattern> depending on the reference mutability. For the bindings you need to use the inverse. You can leave them as plain bindings if you wish for the value to be copied, but you must use ref mut <variable> or ref <variable> to construct a reference into the matched structure. If you are looking for a way to learn about ownership semantics in more detail, it is recommended to look at IDE options available to you to highlight types, lifetimes and reference semantics in your code. The available tooling would expose these things in a general way even outside of the various pattern matching mechanics. Of course this lint can still be used to highlight areas of interest and ensure a good understanding of ownership semantics. It is in the restriction group and off by default.
What you see
warning: type of pattern does not match the expression type --> main.rs:6:5 | 6 | Some(inner) => println!("{}", inner), | ^^^^^^^^^^^ | = help: use `*` to dereference the match expression or explicitly match against a `&_` pattern and adjust the enclosed variable bindings = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#pattern_type_mismatch = note: requested on the command line with `-W clippy::pattern-type-mismatch`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
It increases ownership hints in the code, and will guard against some changes in ownership.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
// Bad
let value = &Some(Box::new(23));
match value {
Some(inner) => println!("{}", inner),
None => println!("none"),
}
// Good
let value = &Some(Box::new(23));
match *value {
Some(ref inner) => println!("{}", inner),
None => println!("none"),
}
Write this instead
// Bad
let mut values = vec![(2, 3), (3, 4)];
for (a, b) in &mut values {
*a += *b;
}
// Good
let mut values = vec![(2, 3), (3, 4)];
for &mut (ref mut a, b) in &mut values {
*a += b;
}
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
pattern_type_mismatch = "warn"In code
#![warn(clippy::pattern_type_mismatch)]One run
cargo clippy -- -W clippy::pattern-type-mismatchThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for patterns that aren’t exact representations of the types they are applied to.
To satisfy this lint, you will have to adjust either the expression that is matched
against or the pattern itself, as well as the bindings that are introduced by the
adjusted patterns. For matching you will have to either dereference the expression
with the * operator, or amend the patterns to explicitly match against &<pattern>
or &mut <pattern> depending on the reference mutability. For the bindings you need
to use the inverse. You can leave them as plain bindings if you wish for the value
to be copied, but you must use ref mut <variable> or ref <variable> to construct
a reference into the matched structure.
If you are looking for a way to learn about ownership semantics in more detail, it is recommended to look at IDE options available to you to highlight types, lifetimes and reference semantics in your code. The available tooling would expose these things in a general way even outside of the various pattern matching mechanics. Of course this lint can still be used to highlight areas of interest and ensure a good understanding of ownership semantics.
Why restrict this?
It increases ownership hints in the code, and will guard against some changes in ownership.
Example
This example shows the basic adjustments necessary to satisfy the lint. Note how
the matched expression is explicitly dereferenced with * and the inner variable
is bound to a shared borrow via ref inner.
// Bad
let value = &Some(Box::new(23));
match value {
Some(inner) => println!("{}", inner),
None => println!("none"),
}
// Good
let value = &Some(Box::new(23));
match *value {
Some(ref inner) => println!("{}", inner),
None => println!("none"),
}
The following example demonstrates one of the advantages of the more verbose style.
Note how the second version uses ref mut a to explicitly declare a a shared mutable
borrow, while b is simply taken by value. This ensures that the loop body cannot
accidentally modify the wrong part of the structure.
// Bad
let mut values = vec![(2, 3), (3, 4)];
for (a, b) in &mut values {
*a += *b;
}
// Good
let mut values = vec![(2, 3), (3, 4)];
for &mut (ref mut a, b) in &mut values {
*a += b;
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- panic_in_result_fnused `panic!()` or assertion in a function that returns `Result`
- partial_pub_fieldsmixed usage of pub and non-pub fields
- pathbuf_init_then_pushChecks for calls to push immediately after creating a new PathBuf
- pointer_formatpointer formatting detected
- precedence_bitsChecks for bit shifting operations combined with bit masking/combining operators and suggest using parentheses
- print_stderruse of `eprintln!`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?