Skip to content

Commit a63308b

Browse files
committed
Auto merge of rust-lang#8619 - pitaj:fix-6973, r=giraffate
ignore `&x | &y` in unnested_or_patterns replacing it with `&(x | y)` is actually more characters Fixes rust-lang#6973 changelog: [`unnested_or_patterns`] ignore `&x | &y`, nesting would result in more characters
2 parents 984330a + c70f1e0 commit a63308b

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

clippy_lints/src/unnested_or_patterns.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
55
use clippy_utils::{meets_msrv, msrvs, over};
66
use rustc_ast::mut_visit::*;
77
use rustc_ast::ptr::P;
8-
use rustc_ast::{self as ast, Pat, PatKind, PatKind::*, DUMMY_NODE_ID};
8+
use rustc_ast::{self as ast, Mutability, Pat, PatKind, PatKind::*, DUMMY_NODE_ID};
99
use rustc_ast_pretty::pprust;
1010
use rustc_errors::Applicability;
1111
use rustc_lint::{EarlyContext, EarlyLintPass};
@@ -230,6 +230,10 @@ fn transform_with_focus_on_idx(alternatives: &mut Vec<P<Pat>>, focus_idx: usize)
230230
// with which a pattern `C(p_0)` may be formed,
231231
// which we would want to join with other `C(p_j)`s.
232232
Ident(.., None) | Lit(_) | Wild | Path(..) | Range(..) | Rest | MacCall(_)
233+
// Skip immutable refs, as grouping them saves few characters,
234+
// and almost always requires adding parens (increasing noisiness).
235+
// In the case of only two patterns, replacement adds net characters.
236+
| Ref(_, Mutability::Not)
233237
// Dealt with elsewhere.
234238
| Or(_) | Paren(_) => false,
235239
// Transform `box x | ... | box y` into `box (x | y)`.
@@ -241,10 +245,10 @@ fn transform_with_focus_on_idx(alternatives: &mut Vec<P<Pat>>, focus_idx: usize)
241245
|k| matches!(k, Box(_)),
242246
|k| always_pat!(k, Box(p) => p),
243247
),
244-
// Transform `&m x | ... | &m y` into `&m (x | y)`.
245-
Ref(target, m1) => extend_with_matching(
248+
// Transform `&mut x | ... | &mut y` into `&mut (x | y)`.
249+
Ref(target, Mutability::Mut) => extend_with_matching(
246250
target, start, alternatives,
247-
|k| matches!(k, Ref(_, m2) if m1 == m2), // Mutabilities must match.
251+
|k| matches!(k, Ref(_, Mutability::Mut)),
248252
|k| always_pat!(k, Ref(p, _) => p),
249253
),
250254
// Transform `b @ p0 | ... b @ p1` into `b @ (p0 | p1)`.

tests/ui/unnested_or_patterns.fixed

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
#![allow(unreachable_patterns, irrefutable_let_patterns, unused_variables)]
77

88
fn main() {
9+
// Should be ignored by this lint, as nesting requires more characters.
10+
if let &0 | &2 = &0 {}
11+
912
if let box (0 | 2) = Box::new(0) {}
1013
if let box (0 | 1 | 2 | 3 | 4) = Box::new(0) {}
11-
const C0: &u8 = &1;
12-
if let &(0 | 2) | C0 = &0 {}
14+
const C0: Option<u8> = Some(1);
15+
if let Some(1 | 2) | C0 = None {}
1316
if let &mut (0 | 2) = &mut 0 {}
1417
if let x @ (0 | 2) = 0 {}
1518
if let (0, 1 | 2 | 3) = (0, 0) {}

tests/ui/unnested_or_patterns.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
#![allow(unreachable_patterns, irrefutable_let_patterns, unused_variables)]
77

88
fn main() {
9+
// Should be ignored by this lint, as nesting requires more characters.
10+
if let &0 | &2 = &0 {}
11+
912
if let box 0 | box 2 = Box::new(0) {}
1013
if let box ((0 | 1)) | box (2 | 3) | box 4 = Box::new(0) {}
11-
const C0: &u8 = &1;
12-
if let &0 | C0 | &2 = &0 {}
14+
const C0: Option<u8> = Some(1);
15+
if let Some(1) | C0 | Some(2) = None {}
1316
if let &mut 0 | &mut 2 = &mut 0 {}
1417
if let x @ 0 | x @ 2 = 0 {}
1518
if let (0, 1) | (0, 2) | (0, 3) = (0, 0) {}

tests/ui/unnested_or_patterns.stderr

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: unnested or-patterns
2-
--> $DIR/unnested_or_patterns.rs:9:12
2+
--> $DIR/unnested_or_patterns.rs:12:12
33
|
44
LL | if let box 0 | box 2 = Box::new(0) {}
55
| ^^^^^^^^^^^^^
@@ -11,7 +11,7 @@ LL | if let box (0 | 2) = Box::new(0) {}
1111
| ~~~~~~~~~~~
1212

1313
error: unnested or-patterns
14-
--> $DIR/unnested_or_patterns.rs:10:12
14+
--> $DIR/unnested_or_patterns.rs:13:12
1515
|
1616
LL | if let box ((0 | 1)) | box (2 | 3) | box 4 = Box::new(0) {}
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -22,18 +22,18 @@ LL | if let box (0 | 1 | 2 | 3 | 4) = Box::new(0) {}
2222
| ~~~~~~~~~~~~~~~~~~~~~~~
2323

2424
error: unnested or-patterns
25-
--> $DIR/unnested_or_patterns.rs:12:12
25+
--> $DIR/unnested_or_patterns.rs:15:12
2626
|
27-
LL | if let &0 | C0 | &2 = &0 {}
28-
| ^^^^^^^^^^^^
27+
LL | if let Some(1) | C0 | Some(2) = None {}
28+
| ^^^^^^^^^^^^^^^^^^^^^^
2929
|
3030
help: nest the patterns
3131
|
32-
LL | if let &(0 | 2) | C0 = &0 {}
33-
| ~~~~~~~~~~~~~
32+
LL | if let Some(1 | 2) | C0 = None {}
33+
| ~~~~~~~~~~~~~~~~
3434

3535
error: unnested or-patterns
36-
--> $DIR/unnested_or_patterns.rs:13:12
36+
--> $DIR/unnested_or_patterns.rs:16:12
3737
|
3838
LL | if let &mut 0 | &mut 2 = &mut 0 {}
3939
| ^^^^^^^^^^^^^^^
@@ -44,7 +44,7 @@ LL | if let &mut (0 | 2) = &mut 0 {}
4444
| ~~~~~~~~~~~~
4545

4646
error: unnested or-patterns
47-
--> $DIR/unnested_or_patterns.rs:14:12
47+
--> $DIR/unnested_or_patterns.rs:17:12
4848
|
4949
LL | if let x @ 0 | x @ 2 = 0 {}
5050
| ^^^^^^^^^^^^^
@@ -55,7 +55,7 @@ LL | if let x @ (0 | 2) = 0 {}
5555
| ~~~~~~~~~~~
5656

5757
error: unnested or-patterns
58-
--> $DIR/unnested_or_patterns.rs:15:12
58+
--> $DIR/unnested_or_patterns.rs:18:12
5959
|
6060
LL | if let (0, 1) | (0, 2) | (0, 3) = (0, 0) {}
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -66,7 +66,7 @@ LL | if let (0, 1 | 2 | 3) = (0, 0) {}
6666
| ~~~~~~~~~~~~~~
6767

6868
error: unnested or-patterns
69-
--> $DIR/unnested_or_patterns.rs:16:12
69+
--> $DIR/unnested_or_patterns.rs:19:12
7070
|
7171
LL | if let (1, 0) | (2, 0) | (3, 0) = (0, 0) {}
7272
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -77,7 +77,7 @@ LL | if let (1 | 2 | 3, 0) = (0, 0) {}
7777
| ~~~~~~~~~~~~~~
7878

7979
error: unnested or-patterns
80-
--> $DIR/unnested_or_patterns.rs:17:12
80+
--> $DIR/unnested_or_patterns.rs:20:12
8181
|
8282
LL | if let (x, ..) | (x, 1) | (x, 2) = (0, 1) {}
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -88,7 +88,7 @@ LL | if let (x, ..) | (x, 1 | 2) = (0, 1) {}
8888
| ~~~~~~~~~~~~~~~~~~~~
8989

9090
error: unnested or-patterns
91-
--> $DIR/unnested_or_patterns.rs:18:12
91+
--> $DIR/unnested_or_patterns.rs:21:12
9292
|
9393
LL | if let [0] | [1] = [0] {}
9494
| ^^^^^^^^^
@@ -99,7 +99,7 @@ LL | if let [0 | 1] = [0] {}
9999
| ~~~~~~~
100100

101101
error: unnested or-patterns
102-
--> $DIR/unnested_or_patterns.rs:19:12
102+
--> $DIR/unnested_or_patterns.rs:22:12
103103
|
104104
LL | if let [x, 0] | [x, 1] = [0, 1] {}
105105
| ^^^^^^^^^^^^^^^
@@ -110,7 +110,7 @@ LL | if let [x, 0 | 1] = [0, 1] {}
110110
| ~~~~~~~~~~
111111

112112
error: unnested or-patterns
113-
--> $DIR/unnested_or_patterns.rs:20:12
113+
--> $DIR/unnested_or_patterns.rs:23:12
114114
|
115115
LL | if let [x, 0] | [x, 1] | [x, 2] = [0, 1] {}
116116
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -121,7 +121,7 @@ LL | if let [x, 0 | 1 | 2] = [0, 1] {}
121121
| ~~~~~~~~~~~~~~
122122

123123
error: unnested or-patterns
124-
--> $DIR/unnested_or_patterns.rs:21:12
124+
--> $DIR/unnested_or_patterns.rs:24:12
125125
|
126126
LL | if let [x, ..] | [x, 1] | [x, 2] = [0, 1] {}
127127
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -132,7 +132,7 @@ LL | if let [x, ..] | [x, 1 | 2] = [0, 1] {}
132132
| ~~~~~~~~~~~~~~~~~~~~
133133

134134
error: unnested or-patterns
135-
--> $DIR/unnested_or_patterns.rs:23:12
135+
--> $DIR/unnested_or_patterns.rs:26:12
136136
|
137137
LL | if let TS(0, x) | TS(1, x) = TS(0, 0) {}
138138
| ^^^^^^^^^^^^^^^^^^^
@@ -143,7 +143,7 @@ LL | if let TS(0 | 1, x) = TS(0, 0) {}
143143
| ~~~~~~~~~~~~
144144

145145
error: unnested or-patterns
146-
--> $DIR/unnested_or_patterns.rs:24:12
146+
--> $DIR/unnested_or_patterns.rs:27:12
147147
|
148148
LL | if let TS(1, 0) | TS(2, 0) | TS(3, 0) = TS(0, 0) {}
149149
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -154,7 +154,7 @@ LL | if let TS(1 | 2 | 3, 0) = TS(0, 0) {}
154154
| ~~~~~~~~~~~~~~~~
155155

156156
error: unnested or-patterns
157-
--> $DIR/unnested_or_patterns.rs:25:12
157+
--> $DIR/unnested_or_patterns.rs:28:12
158158
|
159159
LL | if let TS(x, ..) | TS(x, 1) | TS(x, 2) = TS(0, 0) {}
160160
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -165,7 +165,7 @@ LL | if let TS(x, ..) | TS(x, 1 | 2) = TS(0, 0) {}
165165
| ~~~~~~~~~~~~~~~~~~~~~~~~
166166

167167
error: unnested or-patterns
168-
--> $DIR/unnested_or_patterns.rs:30:12
168+
--> $DIR/unnested_or_patterns.rs:33:12
169169
|
170170
LL | if let S { x: 0, y } | S { y, x: 1 } = (S { x: 0, y: 1 }) {}
171171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)