@@ -14,6 +14,22 @@ declare_clippy_lint! {
14
14
/// **What it does:** Checks for patterns that aren't exact representations of the types
15
15
/// they are applied to.
16
16
///
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
+ ///
17
33
/// **Why is this bad?** It isn't bad in general. But in some contexts it can be desirable
18
34
/// because it increases ownership hints in the code, and will guard against some changes
19
35
/// in ownership.
@@ -22,6 +38,10 @@ declare_clippy_lint! {
22
38
///
23
39
/// **Example:**
24
40
///
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
+ ///
25
45
/// ```rust,ignore
26
46
/// // Bad
27
47
/// let value = &Some(Box::new(23));
@@ -37,6 +57,25 @@ declare_clippy_lint! {
37
57
/// None => println!("none"),
38
58
/// }
39
59
/// ```
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
+ /// ```
40
79
pub PATTERN_TYPE_MISMATCH ,
41
80
restriction,
42
81
"type of pattern does not match the expression type"
0 commit comments