]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/unnested_or_patterns.rs
Merge remote-tracking branch 'upstream/master' into rustup
[rust.git] / clippy_lints / src / unnested_or_patterns.rs
index 2723af03c0b9865a733f3b730905aade4bf6c140..7f4f16f8faf96225c201528e2d1f9a3804490da8 100644 (file)
@@ -2,9 +2,9 @@
 
 use crate::utils::ast_utils::{eq_field_pat, eq_id, eq_pat, eq_path};
 use crate::utils::{over, span_lint_and_then};
-use rustc_ast::ast::{self, Pat, PatKind, PatKind::*, DUMMY_NODE_ID};
 use rustc_ast::mut_visit::*;
 use rustc_ast::ptr::P;
+use rustc_ast::{self as ast, Pat, PatKind, PatKind::*, DUMMY_NODE_ID};
 use rustc_ast_pretty::pprust;
 use rustc_errors::Applicability;
 use rustc_lint::{EarlyContext, EarlyLintPass};
@@ -45,7 +45,7 @@
     /// }
     /// ```
     pub UNNESTED_OR_PATTERNS,
-    complexity,
+    pedantic,
     "unnested or-patterns, e.g., `Foo(Bar) | Foo(Baz) instead of `Foo(Bar | Baz)`"
 }
 
@@ -72,8 +72,8 @@ fn check_local(&mut self, cx: &EarlyContext<'_>, l: &ast::Local) {
 }
 
 fn lint_unnested_or_patterns(cx: &EarlyContext<'_>, pat: &Pat) {
-    if !cx.sess.opts.unstable_features.is_nightly_build() {
-        // User cannot do `#![feature(or_patterns)]`, so bail.
+    if !cx.sess.features_untracked().or_patterns {
+        // Do not suggest nesting the patterns if the feature `or_patterns` is not enabled.
         return;
     }
 
@@ -214,21 +214,21 @@ fn transform_with_focus_on_idx(alternatives: &mut Vec<P<Pat>>, focus_idx: usize)
     let changed = match &mut focus_kind {
         // These pattern forms are "leafs" and do not have sub-patterns.
         // Therefore they are not some form of constructor `C`,
-        // with which a pattern `C(P0)` may be formed,
-        // which we would want to join with other `C(Pj)`s.
+        // with which a pattern `C(p_0)` may be formed,
+        // which we would want to join with other `C(p_j)`s.
         Ident(.., None) | Lit(_) | Wild | Path(..) | Range(..) | Rest | MacCall(_)
         // Dealt with elsewhere.
         | Or(_) | Paren(_) => false,
         // Transform `box x | ... | box y` into `box (x | y)`.
         //
-        // The cases below until `Slice(...)` deal *singleton* products.
+        // The cases below until `Slice(...)` deal with *singleton* products.
         // These patterns have the shape `C(p)`, and not e.g., `C(p0, ..., pn)`.
         Box(target) => extend_with_matching(
             target, start, alternatives,
             |k| matches!(k, Box(_)),
             |k| always_pat!(k, Box(p) => p),
         ),
-        // Transform `&m x | ... | &m y` into `&m (x, y)`.
+        // Transform `&m x | ... | &m y` into `&m (x | y)`.
         Ref(target, m1) => extend_with_matching(
             target, start, alternatives,
             |k| matches!(k, Ref(_, m2) if m1 == m2), // Mutabilities must match.
@@ -247,13 +247,13 @@ fn transform_with_focus_on_idx(alternatives: &mut Vec<P<Pat>>, focus_idx: usize)
             |k, ps1, idx| matches!(k, Slice(ps2) if eq_pre_post(ps1, ps2, idx)),
             |k| always_pat!(k, Slice(ps) => ps),
         ),
-        // Transform `(pre, x, post) | ... | (pre, y, post)` into `(pre, x | y, post]`.
+        // Transform `(pre, x, post) | ... | (pre, y, post)` into `(pre, x | y, post)`.
         Tuple(ps1) => extend_with_matching_product(
             ps1, start, alternatives,
             |k, ps1, idx| matches!(k, Tuple(ps2) if eq_pre_post(ps1, ps2, idx)),
             |k| always_pat!(k, Tuple(ps) => ps),
         ),
-        // Transform `S(pre, x, post) | ... | S(pre, y, post)` into `S(pre, x | y, post]`.
+        // Transform `S(pre, x, post) | ... | S(pre, y, post)` into `S(pre, x | y, post)`.
         TupleStruct(path1, ps1) => extend_with_matching_product(
             ps1, start, alternatives,
             |k, ps1, idx| matches!(
@@ -340,6 +340,7 @@ fn take_pat(from: &mut Pat) -> Pat {
         id: DUMMY_NODE_ID,
         kind: Wild,
         span: DUMMY_SP,
+        tokens: None,
     };
     mem::replace(from, dummy)
 }
@@ -400,8 +401,8 @@ fn extend_with_matching(
 
 /// Are the patterns in `ps1` and `ps2` equal save for `ps1[idx]` compared to `ps2[idx]`?
 fn eq_pre_post(ps1: &[P<Pat>], ps2: &[P<Pat>], idx: usize) -> bool {
-    ps1[idx].is_rest() == ps2[idx].is_rest() // Avoid `[x, ..] | [x, 0]` => `[x, .. | 0]`.
-        && ps1.len() == ps2.len()
+    ps1.len() == ps2.len()
+        && ps1[idx].is_rest() == ps2[idx].is_rest() // Avoid `[x, ..] | [x, 0]` => `[x, .. | 0]`.
         && over(&ps1[..idx], &ps2[..idx], |l, r| eq_pat(l, r))
         && over(&ps1[idx + 1..], &ps2[idx + 1..], |l, r| eq_pat(l, r))
 }