fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
4.3 KiB128 linesraw
1// run-rustfix
2
3#![feature(box_patterns, stmt_expr_attributes, yeet_expr)]
4
5#![allow(
6    dead_code,
7    ellipsis_inclusive_range_patterns,
8    irrefutable_let_patterns,
9    unreachable_patterns,
10    unused_mut,
11    unused_variables
12)]
13#![deny(unused_parens)]
14
15fn lint_on_top_level() {
16    let a = 0; //~ ERROR unnecessary parentheses around pattern
17    for a in 0..1 {} //~ ERROR unnecessary parentheses around pattern
18    if let a = 0 {} //~ ERROR unnecessary parentheses around pattern
19    while let a = 0 {} //~ ERROR unnecessary parentheses around pattern
20    fn foo(a: u8) {} //~ ERROR unnecessary parentheses around pattern
21    let _ = |a: u8| 0; //~ ERROR unnecessary parentheses around pattern
22}
23
24fn no_lint_attr() {
25    let _x = #[allow(dead_code)] (1 + 2);
26}
27
28fn no_lint_yeet() -> Result<(), ()> {
29    #[allow(unreachable_code)]
30    if (do yeet) {}
31
32    Ok(())
33}
34
35fn no_lint_ops() {
36    #![allow(unreachable_code, irrefutable_let_patterns)]
37    if ((..{}) == ..{}) {}
38    if (!return) {}
39    loop { match (() = () = () = break {}) {} }
40    while let () = (*&mut false |= true && return) {}
41}
42
43fn lint_break_if_not_followed_by_block() {
44    #![allow(unreachable_code)]
45    loop { if break {} } //~ ERROR unnecessary parentheses
46    loop { if break ({ println!("hello") }) {} } //~ ERROR unnecessary parentheses
47    loop { if (break { println!("hello") }) {} }
48}
49
50// Don't lint in these cases (#64106).
51fn or_patterns_no_lint() {
52    match Box::new(0) {
53        box (0 | 1) => {} // Should not lint as `box 0 | 1` binds as `(box 0) | 1`.
54        _ => {}
55    }
56
57    match 0 {
58        x @ (0 | 1) => {} // Should not lint as `x @ 0 | 1` binds as `(x @ 0) | 1`.
59        _ => {}
60    }
61
62    if let &(0 | 1) = &0 {} // Should also not lint.
63    if let &mut (0 | 1) = &mut 0 {} // Same.
64
65    fn foo((Ok(a) | Err(a)): Result<u8, u8>) {} // Doesn't parse if we remove parens for now.
66
67    let _ = |(Ok(a) | Err(a)): Result<u8, u8>| 1; // `|Ok(a) | Err(a)| 1` parses as bit-or.
68}
69
70fn or_patterns_will_lint() {
71    if let 0 | 1 = 0 {} //~ ERROR unnecessary parentheses around pattern
72    if let (0 | 1,) = (0,) {} //~ ERROR unnecessary parentheses around pattern
73    if let [0 | 1] = [0] {} //~ ERROR unnecessary parentheses around pattern
74    if let 0 | 1 | 2 = 0 {} //~ ERROR unnecessary parentheses around pattern
75    struct TS(u8);
76    if let TS(0 | 1) = TS(0) {} //~ ERROR unnecessary parentheses around pattern
77    struct NS { f: u8 }
78    if let NS { f: 0 | 1 } = (NS { f: 0 }) {} //~ ERROR unnecessary parentheses around pattern
79}
80
81// Don't lint on `&(mut x)` because `&mut x` means something else (#55342).
82fn deref_mut_binding_no_lint() {
83    let &(mut x) = &0;
84}
85
86fn main() {
87    match 1 {
88        _ => {} //~ ERROR unnecessary parentheses around pattern
89        y => {} //~ ERROR unnecessary parentheses around pattern
90        ref r => {} //~ ERROR unnecessary parentheses around pattern
91        e @ 1...2 => {} //~ ERROR unnecessary parentheses around pattern
92        (1...2) => {} // Non ambiguous range pattern should not warn
93        e @ (3...4) => {} // Non ambiguous range pattern should not warn
94    }
95
96    match &1 {
97        e @ &(1...2) => {} //~ ERROR unnecessary parentheses around pattern
98        &_ => {} //~ ERROR unnecessary parentheses around pattern
99        e @ &(1...2) => {} // Ambiguous range pattern should not warn
100        &(1...2) => {} // Ambiguous range pattern should not warn
101    }
102
103    match &1 {
104        e @ &(1...2) | e @ &(3...4) => {} // Complex ambiguous pattern should not warn
105        &_ => {}
106    }
107
108    match 1 {
109        _ => {} //~ ERROR unnecessary parentheses around pattern
110        y => {} //~ ERROR unnecessary parentheses around pattern
111        ref r => {} //~ ERROR unnecessary parentheses around pattern
112        e @ 1..=2 => {} //~ ERROR unnecessary parentheses around pattern
113        (1..=2) => {} // Non ambiguous range pattern should not warn
114        e @ (3..=4) => {} // Non ambiguous range pattern should not warn
115    }
116
117    match &1 {
118        e @ &(1..=2) => {} //~ ERROR unnecessary parentheses around pattern
119        &_ => {} //~ ERROR unnecessary parentheses around pattern
120        e @ &(1..=2) => {} // Ambiguous range pattern should not warn
121        &(1..=2) => {} // Ambiguous range pattern should not warn
122    }
123
124    match &1 {
125        e @ &(1..=2) | e @ &(3..=4) => {} // Complex ambiguous pattern should not warn
126        &_ => {}
127    }
128}