Skip to content

Commit 77b6130

Browse files
committed
Expanded lint documentation
1 parent 004bb70 commit 77b6130

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

clippy_lints/src/pattern_type_mismatch.rs

+39
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ declare_clippy_lint! {
1414
/// **What it does:** Checks for patterns that aren't exact representations of the types
1515
/// they are applied to.
1616
///
17+
/// To satisfy this lint, you will have to adjust either the expression that is matched
18+
/// against or the pattern itself, as well as the bindings that are introduced by the
19+
/// adjusted patterns. For matching you will have to either dereference the expression
20+
/// with the `*` operator, or amend the patterns to explicitly match against `&<pattern>`
21+
/// or `&mut <pattern>` depending on the reference mutability. For the bindings you need
22+
/// to use the inverse. You can leave them as plain bindings if you wish for the value
23+
/// to be copied, but you must use `ref mut <variable>` or `ref <variable>` to construct
24+
/// a reference into the matched structure.
25+
///
26+
/// If you are looking for a way to learn about ownership semantics in more detail, it
27+
/// is recommended to look at IDE options available to you to highlight types, lifetimes
28+
/// and reference semantics in your code. The available tooling would expose these things
29+
/// in a general way even outside of the various pattern matching mechanics. Of course
30+
/// this lint can still be used to highlight areas of interest and ensure a good understanding
31+
/// of ownership semantics.
32+
///
1733
/// **Why is this bad?** It isn't bad in general. But in some contexts it can be desirable
1834
/// because it increases ownership hints in the code, and will guard against some changes
1935
/// in ownership.
@@ -22,6 +38,10 @@ declare_clippy_lint! {
2238
///
2339
/// **Example:**
2440
///
41+
/// This example shows the basic adjustments necessary to satisfy the lint. Note how
42+
/// the matched expression is explicitly dereferenced with `*` and the `inner` variable
43+
/// is bound to a shared borrow via `ref inner`.
44+
///
2545
/// ```rust,ignore
2646
/// // Bad
2747
/// let value = &Some(Box::new(23));
@@ -37,6 +57,25 @@ declare_clippy_lint! {
3757
/// None => println!("none"),
3858
/// }
3959
/// ```
60+
///
61+
/// The following example demonstrates one of the advantages of the more verbose style.
62+
/// Note how the second version uses `ref mut a` to explicitly declare `a` a shared mutable
63+
/// borrow, while `b` is simply taken by value. This ensures that the loop body cannot
64+
/// accidentally modify the wrong part of the structure.
65+
///
66+
/// ```rust,ignore
67+
/// // Bad
68+
/// let mut values = vec![(2, 3), (3, 4)];
69+
/// for (a, b) in &mut values {
70+
/// *a += *b;
71+
/// }
72+
///
73+
/// // Good
74+
/// let mut values = vec![(2, 3), (3, 4)];
75+
/// for &mut (ref mut a, b) in &mut values {
76+
/// *a += b;
77+
/// }
78+
/// ```
4079
pub PATTERN_TYPE_MISMATCH,
4180
restriction,
4281
"type of pattern does not match the expression type"

0 commit comments

Comments
 (0)