forked from rust-lang/rustfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
let_chain_style
configuration option
Now users have some control over how `let-chains` are formatted. The default value of `LegibleBindings` follows the style guide perscription defined in `r-l/rust 110568`. The `Tall` varient provides users an option to format all chain items on a single line if they fit.
- Loading branch information
Showing
8 changed files
with
332 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
tests/source/configs/let_chain_style/legible_bindings.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// rustfmt-let_chain_style: LegibleBindings | ||
|
||
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 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 { | ||
|
||
} | ||
|
||
// bool literal | ||
if true && 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 { | ||
|
||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
tests/source/let_chains.rs → tests/source/configs/let_chain_style/tall.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// rustfmt-let_chain_style: Tall | ||
|
||
fn main() { | ||
if let x = x && x {} | ||
|
||
|
2 changes: 2 additions & 0 deletions
2
tests/target/let_chains.rs → ...nfigs/let_chain_style/legible_bindings.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// rustfmt-let_chain_style: LegibleBindings | ||
|
||
fn main() { | ||
if let x = x | ||
&& x | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// rustfmt-let_chain_style: Tall | ||
|
||
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 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 {} | ||
|
||
// bool literal | ||
if true && 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 {} | ||
} |