Skip to content

Commit f51c555

Browse files
authored
clean-up unnested_or_patterns (#15344)
misc stuff I found while working on #15343. Not sure about the `P` part, but the rest should be fine changelog: none
2 parents e629869 + 44e3b16 commit f51c555

File tree

1 file changed

+20
-32
lines changed

1 file changed

+20
-32
lines changed

clippy_lints/src/unnested_or_patterns.rs

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_ast::mut_visit::*;
1010
use rustc_ast::{self as ast, DUMMY_NODE_ID, Mutability, Pat, PatKind};
1111
use rustc_ast_pretty::pprust;
1212
use rustc_data_structures::thin_vec::{ThinVec, thin_vec};
13+
use rustc_data_structures::thinvec::ExtractIf;
1314
use rustc_errors::Applicability;
1415
use rustc_lint::{EarlyContext, EarlyLintPass};
1516
use rustc_session::impl_lint_pass;
@@ -98,7 +99,7 @@ fn lint_unnested_or_patterns(cx: &EarlyContext<'_>, pat: &Pat) {
9899
return;
99100
}
100101

101-
let mut pat = Box::new(pat.clone());
102+
let mut pat = pat.clone();
102103

103104
// Nix all the paren patterns everywhere so that they aren't in our way.
104105
remove_all_parens(&mut pat);
@@ -120,7 +121,7 @@ fn lint_unnested_or_patterns(cx: &EarlyContext<'_>, pat: &Pat) {
120121
}
121122

122123
/// Remove all `(p)` patterns in `pat`.
123-
fn remove_all_parens(pat: &mut Box<Pat>) {
124+
fn remove_all_parens(pat: &mut Pat) {
124125
#[derive(Default)]
125126
struct Visitor {
126127
/// If is not in the outer most pattern. This is needed to avoid removing the outermost
@@ -143,7 +144,7 @@ fn remove_all_parens(pat: &mut Box<Pat>) {
143144
}
144145

145146
/// Insert parens where necessary according to Rust's precedence rules for patterns.
146-
fn insert_necessary_parens(pat: &mut Box<Pat>) {
147+
fn insert_necessary_parens(pat: &mut Pat) {
147148
struct Visitor;
148149
impl MutVisitor for Visitor {
149150
fn visit_pat(&mut self, pat: &mut Pat) {
@@ -163,7 +164,7 @@ fn insert_necessary_parens(pat: &mut Box<Pat>) {
163164

164165
/// Unnest or-patterns `p0 | ... | p1` in the pattern `pat`.
165166
/// For example, this would transform `Some(0) | FOO | Some(2)` into `Some(0 | 2) | FOO`.
166-
fn unnest_or_patterns(pat: &mut Box<Pat>) -> bool {
167+
fn unnest_or_patterns(pat: &mut Pat) -> bool {
167168
struct Visitor {
168169
changed: bool,
169170
}
@@ -384,15 +385,14 @@ fn take_pat(from: &mut Pat) -> Pat {
384385
/// in `tail_or` if there are any and return if there were.
385386
fn extend_with_tail_or(target: &mut Pat, tail_or: ThinVec<Pat>) -> bool {
386387
fn extend(target: &mut Pat, mut tail_or: ThinVec<Pat>) {
387-
match target {
388-
// On an existing or-pattern in the target, append to it.
389-
Pat { kind: Or(ps), .. } => ps.append(&mut tail_or),
390-
// Otherwise convert the target to an or-pattern.
391-
target => {
392-
let mut init_or = thin_vec![take_pat(target)];
393-
init_or.append(&mut tail_or);
394-
target.kind = Or(init_or);
395-
},
388+
// On an existing or-pattern in the target, append to it,
389+
// otherwise convert the target to an or-pattern.
390+
if let Or(ps) = &mut target.kind {
391+
ps.append(&mut tail_or);
392+
} else {
393+
let mut init_or = thin_vec![take_pat(target)];
394+
init_or.append(&mut tail_or);
395+
target.kind = Or(init_or);
396396
}
397397
}
398398

@@ -415,26 +415,14 @@ fn drain_matching(
415415
let mut tail_or = ThinVec::new();
416416
let mut idx = 0;
417417

418-
// If `ThinVec` had the `drain_filter` method, this loop could be rewritten
419-
// like so:
420-
//
421-
// for pat in alternatives.drain_filter(|p| {
422-
// // Check if we should extract, but only if `idx >= start`.
423-
// idx += 1;
424-
// idx > start && predicate(&p.kind)
425-
// }) {
426-
// tail_or.push(extract(pat.into_inner().kind));
427-
// }
428-
let mut i = 0;
429-
while i < alternatives.len() {
430-
idx += 1;
418+
// FIXME: once `thin-vec` releases a new version, change this to `alternatives.extract_if()`
419+
// See https://github.com/mozilla/thin-vec/issues/77
420+
for pat in ExtractIf::new(alternatives, |p| {
431421
// Check if we should extract, but only if `idx >= start`.
432-
if idx > start && predicate(&alternatives[i].kind) {
433-
let pat = alternatives.remove(i);
434-
tail_or.push(extract(pat.kind));
435-
} else {
436-
i += 1;
437-
}
422+
idx += 1;
423+
idx > start && predicate(&p.kind)
424+
}) {
425+
tail_or.push(extract(pat.kind));
438426
}
439427

440428
tail_or

0 commit comments

Comments
 (0)