summaryrefslogtreecommitdiffstats
path: root/tests/ui/lint/unused/issue-54538-unused-parens-lint.rs
blob: 28b662dd0242c8715c7ba5e8e22d34426f8537a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// run-rustfix

#![feature(box_patterns, stmt_expr_attributes, yeet_expr)]

#![allow(
    dead_code,
    ellipsis_inclusive_range_patterns,
    irrefutable_let_patterns,
    unreachable_patterns,
    unused_mut,
    unused_variables
)]
#![deny(unused_parens)]

fn lint_on_top_level() {
    let (a) = 0; //~ ERROR unnecessary parentheses around pattern
    for (a) in 0..1 {} //~ ERROR unnecessary parentheses around pattern
    if let (a) = 0 {} //~ ERROR unnecessary parentheses around pattern
    while let (a) = 0 {} //~ ERROR unnecessary parentheses around pattern
    fn foo((a): u8) {} //~ ERROR unnecessary parentheses around pattern
    let _ = |(a): u8| 0; //~ ERROR unnecessary parentheses around pattern
}

fn _no_lint_attr() {
    let _x = #[allow(dead_code)] (1 + 2);
}

fn _no_lint_yeet() -> Result<(), ()> {
    #[allow(unreachable_code)]
    if (do yeet) {}

    Ok(())
}

// Don't lint in these cases (#64106).
fn or_patterns_no_lint() {
    match Box::new(0) {
        box (0 | 1) => {} // Should not lint as `box 0 | 1` binds as `(box 0) | 1`.
        _ => {}
    }

    match 0 {
        x @ (0 | 1) => {} // Should not lint as `x @ 0 | 1` binds as `(x @ 0) | 1`.
        _ => {}
    }

    if let &(0 | 1) = &0 {} // Should also not lint.
    if let &mut (0 | 1) = &mut 0 {} // Same.

    fn foo((Ok(a) | Err(a)): Result<u8, u8>) {} // Doesn't parse if we remove parens for now.

    let _ = |(Ok(a) | Err(a)): Result<u8, u8>| 1; // `|Ok(a) | Err(a)| 1` parses as bit-or.
}

fn or_patterns_will_lint() {
    if let (0 | 1) = 0 {} //~ ERROR unnecessary parentheses around pattern
    if let ((0 | 1),) = (0,) {} //~ ERROR unnecessary parentheses around pattern
    if let [(0 | 1)] = [0] {} //~ ERROR unnecessary parentheses around pattern
    if let 0 | (1 | 2) = 0 {} //~ ERROR unnecessary parentheses around pattern
    struct TS(u8);
    if let TS((0 | 1)) = TS(0) {} //~ ERROR unnecessary parentheses around pattern
    struct NS { f: u8 }
    if let NS { f: (0 | 1) } = (NS { f: 0 }) {} //~ ERROR unnecessary parentheses around pattern
}

// Don't lint on `&(mut x)` because `&mut x` means something else (#55342).
fn deref_mut_binding_no_lint() {
    let &(mut x) = &0;
}

fn main() {
    match 1 {
        (_) => {} //~ ERROR unnecessary parentheses around pattern
        (y) => {} //~ ERROR unnecessary parentheses around pattern
        (ref r) => {} //~ ERROR unnecessary parentheses around pattern
        (e @ 1...2) => {} //~ ERROR unnecessary parentheses around pattern
        (1...2) => {} // Non ambiguous range pattern should not warn
        e @ (3...4) => {} // Non ambiguous range pattern should not warn
    }

    match &1 {
        (e @ &(1...2)) => {} //~ ERROR unnecessary parentheses around pattern
        &(_) => {} //~ ERROR unnecessary parentheses around pattern
        e @ &(1...2) => {} // Ambiguous range pattern should not warn
        &(1...2) => {} // Ambiguous range pattern should not warn
    }

    match &1 {
        e @ &(1...2) | e @ &(3...4) => {} // Complex ambiguous pattern should not warn
        &_ => {}
    }

    match 1 {
        (_) => {} //~ ERROR unnecessary parentheses around pattern
        (y) => {} //~ ERROR unnecessary parentheses around pattern
        (ref r) => {} //~ ERROR unnecessary parentheses around pattern
        (e @ 1..=2) => {} //~ ERROR unnecessary parentheses around pattern
        (1..=2) => {} // Non ambiguous range pattern should not warn
        e @ (3..=4) => {} // Non ambiguous range pattern should not warn
    }

    match &1 {
        (e @ &(1..=2)) => {} //~ ERROR unnecessary parentheses around pattern
        &(_) => {} //~ ERROR unnecessary parentheses around pattern
        e @ &(1..=2) => {} // Ambiguous range pattern should not warn
        &(1..=2) => {} // Ambiguous range pattern should not warn
    }

    match &1 {
        e @ &(1..=2) | e @ &(3..=4) => {} // Complex ambiguous pattern should not warn
        &_ => {}
    }
}