@@ -7,13 +7,14 @@ use clippy_utils::msrvs::{self, MsrvStack};
7
7
use clippy_utils:: over;
8
8
use rustc_ast:: PatKind :: * ;
9
9
use rustc_ast:: mut_visit:: * ;
10
- use rustc_ast:: ptr:: P ;
11
10
use rustc_ast:: { self as ast, DUMMY_NODE_ID , Mutability , Pat , PatKind } ;
12
11
use rustc_ast_pretty:: pprust;
13
12
use rustc_errors:: Applicability ;
14
13
use rustc_lint:: { EarlyContext , EarlyLintPass } ;
15
14
use rustc_session:: impl_lint_pass;
16
15
use rustc_span:: DUMMY_SP ;
16
+ // import needed to shadow `PatKind::Box` glob-imported above
17
+ use std:: boxed:: Box ;
17
18
use std:: cell:: Cell ;
18
19
use std:: mem;
19
20
use thin_vec:: { ThinVec , thin_vec} ;
@@ -97,7 +98,7 @@ fn lint_unnested_or_patterns(cx: &EarlyContext<'_>, pat: &Pat) {
97
98
return ;
98
99
}
99
100
100
- let mut pat = P ( pat. clone ( ) ) ;
101
+ let mut pat = Box :: new ( pat. clone ( ) ) ;
101
102
102
103
// Nix all the paren patterns everywhere so that they aren't in our way.
103
104
remove_all_parens ( & mut pat) ;
@@ -119,7 +120,7 @@ fn lint_unnested_or_patterns(cx: &EarlyContext<'_>, pat: &Pat) {
119
120
}
120
121
121
122
/// Remove all `(p)` patterns in `pat`.
122
- fn remove_all_parens ( pat : & mut P < Pat > ) {
123
+ fn remove_all_parens ( pat : & mut Box < Pat > ) {
123
124
#[ derive( Default ) ]
124
125
struct Visitor {
125
126
/// If is not in the outer most pattern. This is needed to avoid removing the outermost
@@ -142,7 +143,7 @@ fn remove_all_parens(pat: &mut P<Pat>) {
142
143
}
143
144
144
145
/// Insert parens where necessary according to Rust's precedence rules for patterns.
145
- fn insert_necessary_parens ( pat : & mut P < Pat > ) {
146
+ fn insert_necessary_parens ( pat : & mut Box < Pat > ) {
146
147
struct Visitor ;
147
148
impl MutVisitor for Visitor {
148
149
fn visit_pat ( & mut self , pat : & mut Pat ) {
@@ -154,15 +155,15 @@ fn insert_necessary_parens(pat: &mut P<Pat>) {
154
155
Ref ( p, Mutability :: Not ) if matches ! ( p. kind, Ident ( BindingMode :: MUT , ..) ) => p, // `&(mut x)`
155
156
_ => return ,
156
157
} ;
157
- target. kind = Paren ( P ( take_pat ( target) ) ) ;
158
+ target. kind = Paren ( Box :: new ( take_pat ( target) ) ) ;
158
159
}
159
160
}
160
161
Visitor . visit_pat ( pat) ;
161
162
}
162
163
163
164
/// Unnest or-patterns `p0 | ... | p1` in the pattern `pat`.
164
165
/// For example, this would transform `Some(0) | FOO | Some(2)` into `Some(0 | 2) | FOO`.
165
- fn unnest_or_patterns ( pat : & mut P < Pat > ) -> bool {
166
+ fn unnest_or_patterns ( pat : & mut Box < Pat > ) -> bool {
166
167
struct Visitor {
167
168
changed : bool ,
168
169
}
@@ -222,7 +223,7 @@ macro_rules! always_pat {
222
223
/// Focus on `focus_idx` in `alternatives`,
223
224
/// attempting to extend it with elements of the same constructor `C`
224
225
/// in `alternatives[focus_idx + 1..]`.
225
- fn transform_with_focus_on_idx ( alternatives : & mut ThinVec < P < Pat > > , focus_idx : usize ) -> bool {
226
+ fn transform_with_focus_on_idx ( alternatives : & mut ThinVec < Box < Pat > > , focus_idx : usize ) -> bool {
226
227
// Extract the kind; we'll need to make some changes in it.
227
228
let mut focus_kind = mem:: replace ( & mut alternatives[ focus_idx] . kind , Wild ) ;
228
229
// We'll focus on `alternatives[focus_idx]`,
@@ -303,12 +304,12 @@ fn transform_with_focus_on_idx(alternatives: &mut ThinVec<P<Pat>>, focus_idx: us
303
304
/// So when we fixate on some `ident_k: pat_k`, we try to find `ident_k` in the other pattern
304
305
/// and check that all `fp_i` where `i ∈ ((0...n) \ k)` between two patterns are equal.
305
306
fn extend_with_struct_pat (
306
- qself1 : Option < & P < ast:: QSelf > > ,
307
+ qself1 : Option < & Box < ast:: QSelf > > ,
307
308
path1 : & ast:: Path ,
308
309
fps1 : & mut [ ast:: PatField ] ,
309
310
rest1 : ast:: PatFieldsRest ,
310
311
start : usize ,
311
- alternatives : & mut ThinVec < P < Pat > > ,
312
+ alternatives : & mut ThinVec < Box < Pat > > ,
312
313
) -> bool {
313
314
( 0 ..fps1. len ( ) ) . any ( |idx| {
314
315
let pos_in_2 = Cell :: new ( None ) ; // The element `k`.
@@ -346,11 +347,11 @@ fn extend_with_struct_pat(
346
347
/// while also requiring `ps1[..n] ~ ps2[..n]` (pre) and `ps1[n + 1..] ~ ps2[n + 1..]` (post),
347
348
/// where `~` denotes semantic equality.
348
349
fn extend_with_matching_product (
349
- targets : & mut [ P < Pat > ] ,
350
+ targets : & mut [ Box < Pat > ] ,
350
351
start : usize ,
351
- alternatives : & mut ThinVec < P < Pat > > ,
352
- predicate : impl Fn ( & PatKind , & [ P < Pat > ] , usize ) -> bool ,
353
- extract : impl Fn ( PatKind ) -> ThinVec < P < Pat > > ,
352
+ alternatives : & mut ThinVec < Box < Pat > > ,
353
+ predicate : impl Fn ( & PatKind , & [ Box < Pat > ] , usize ) -> bool ,
354
+ extract : impl Fn ( PatKind ) -> ThinVec < Box < Pat > > ,
354
355
) -> bool {
355
356
( 0 ..targets. len ( ) ) . any ( |idx| {
356
357
let tail_or = drain_matching (
@@ -377,14 +378,14 @@ fn take_pat(from: &mut Pat) -> Pat {
377
378
378
379
/// Extend `target` as an or-pattern with the alternatives
379
380
/// in `tail_or` if there are any and return if there were.
380
- fn extend_with_tail_or ( target : & mut Pat , tail_or : ThinVec < P < Pat > > ) -> bool {
381
- fn extend ( target : & mut Pat , mut tail_or : ThinVec < P < Pat > > ) {
381
+ fn extend_with_tail_or ( target : & mut Pat , tail_or : ThinVec < Box < Pat > > ) -> bool {
382
+ fn extend ( target : & mut Pat , mut tail_or : ThinVec < Box < Pat > > ) {
382
383
match target {
383
384
// On an existing or-pattern in the target, append to it.
384
385
Pat { kind : Or ( ps) , .. } => ps. append ( & mut tail_or) ,
385
386
// Otherwise convert the target to an or-pattern.
386
387
target => {
387
- let mut init_or = thin_vec ! [ P ( take_pat( target) ) ] ;
388
+ let mut init_or = thin_vec ! [ Box :: new ( take_pat( target) ) ] ;
388
389
init_or. append ( & mut tail_or) ;
389
390
target. kind = Or ( init_or) ;
390
391
} ,
@@ -403,10 +404,10 @@ fn extend_with_tail_or(target: &mut Pat, tail_or: ThinVec<P<Pat>>) -> bool {
403
404
// Only elements beginning with `start` are considered for extraction.
404
405
fn drain_matching (
405
406
start : usize ,
406
- alternatives : & mut ThinVec < P < Pat > > ,
407
+ alternatives : & mut ThinVec < Box < Pat > > ,
407
408
predicate : impl Fn ( & PatKind ) -> bool ,
408
- extract : impl Fn ( PatKind ) -> P < Pat > ,
409
- ) -> ThinVec < P < Pat > > {
409
+ extract : impl Fn ( PatKind ) -> Box < Pat > ,
410
+ ) -> ThinVec < Box < Pat > > {
410
411
let mut tail_or = ThinVec :: new ( ) ;
411
412
let mut idx = 0 ;
412
413
@@ -438,15 +439,15 @@ fn drain_matching(
438
439
fn extend_with_matching (
439
440
target : & mut Pat ,
440
441
start : usize ,
441
- alternatives : & mut ThinVec < P < Pat > > ,
442
+ alternatives : & mut ThinVec < Box < Pat > > ,
442
443
predicate : impl Fn ( & PatKind ) -> bool ,
443
- extract : impl Fn ( PatKind ) -> P < Pat > ,
444
+ extract : impl Fn ( PatKind ) -> Box < Pat > ,
444
445
) -> bool {
445
446
extend_with_tail_or ( target, drain_matching ( start, alternatives, predicate, extract) )
446
447
}
447
448
448
449
/// Are the patterns in `ps1` and `ps2` equal save for `ps1[idx]` compared to `ps2[idx]`?
449
- fn eq_pre_post ( ps1 : & [ P < Pat > ] , ps2 : & [ P < Pat > ] , idx : usize ) -> bool {
450
+ fn eq_pre_post ( ps1 : & [ Box < Pat > ] , ps2 : & [ Box < Pat > ] , idx : usize ) -> bool {
450
451
ps1. len ( ) == ps2. len ( )
451
452
&& ps1[ idx] . is_rest ( ) == ps2[ idx] . is_rest ( ) // Avoid `[x, ..] | [x, 0]` => `[x, .. | 0]`.
452
453
&& over ( & ps1[ ..idx] , & ps2[ ..idx] , |l, r| eq_pat ( l, r) )
0 commit comments