about summary refs log tree commit diff
path: root/src/tools/rustfmt/tests/source/let_chains.rs
blob: fc2a9310569150b83ee77645186730b472f428ce (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
114
115
116
117
118
119
120
121
122
123
// rustfmt-edition: 2024

fn main() {
    if let x = x && x {}

    if xxx && let x = x {}

    if aaaaaaaaaaaaaaaaaaaaa &&  aaaaaaaaaaaaaaa && aaaaaaaaa && let Some(x) = xxxxxxxxxxxx && aaaaaaa && let None = aaaaaaaaaa {}

    if aaaaaaaaaaaaaaaaaaaaa &&  aaaaaaaaaaaaaaa && aaaaaaaaa && let Some(x) = xxxxxxxxxxxx && aaaaaaa && let None = aaaaaaaaaa {}

    if let Some(Struct { x:TS(1,2) }) = path::to::<_>(hehe)
        && let [Simple, people] = /* get ready */ create_universe(/* hi */  GreatPowers).initialize_badminton().populate_swamps() &&
        let    everybody    =    (Loops { hi /*hi*/  , ..loopy() }) && summons::triumphantly() { todo!() }

    if let XXXXXXXXX { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, yyyyyyyyyyyyy, zzzzzzzzzzzzz} = xxxxxxx()
    && let Foo = bar() { todo!() }
}

fn test_single_line_let_chain() {
    // first item in let-chain is an ident
    if a && let Some(b) = foo() {
    }

    // first item in let-chain is a bool literal
    if true && let Some(x) = y {

    }

    // first item in let-chain is a unary ! with an ident
    let unary_not = if !from_hir_call
        && let Some(p) = parent
    {
    };

    // first item in let-chain is a unary * with an ident
    let unary_deref = if *some_deref
        && let Some(p) = parent
    {
    };

    // first item in let-chain is a unary - (neg) with an ident
    let unary_neg = if -some_ident
        && let Some(p) = parent
    {
    };

    // first item in let-chain is a try (?) with an ident
    let try_ = if some_try?
        && let Some(p) = parent
    {
    };

    // first item in let-chain is an ident wrapped in parens
    let in_parens = if (some_ident)
        && let Some(p) = parent
    {
    };

    // first item in let-chain is a ref & with an ident
    let _ref = if &some_ref
        && let Some(p) = parent
    {
    };

    // first item in let-chain is a ref &mut with an ident
    let mut_ref = if &mut some_ref
        && let Some(p) = parent
    {
    };

    // chain unary ref and try
    let chain_of_unary_ref_and_try = if !&*some_ref?
        && let Some(p) = parent {
    };
}

fn test_multi_line_let_chain() {
    // Can only single line the let-chain if the first item is an ident
    if let Some(x) = y && a {

    }

    // More than one let-chain must be formatted on multiple lines
    if let Some(x) = y && let Some(a) = b {

    }

    // The ident isn't long enough so we don't wrap the first let-chain
    if a && let Some(x) = y && let Some(a) = b {

    }

    // The ident is long enough so both let-chains are wrapped
    if aaa && let Some(x) = y && let Some(a) = b {

    }

    // function call
    if a() && let Some(x) = y {

    }

    // cast to a bool
    if 1 as bool && let Some(x) = y {

    }

    // matches! macro call
    if matches!(a, some_type) && let Some(x) = y {

    }

    // block expression returning bool
    if { true } && let Some(x) = y {

    }

    // field access
    if a.x && let Some(x) = y {

    }
}