| Crates.io | rustfmt_if_chain |
| lib.rs | rustfmt_if_chain |
| version | 0.1.8 |
| created_at | 2021-12-31 13:51:17.711623+00 |
| updated_at | 2025-04-29 08:26:36.88799+00 |
| description | An if_chain-aware rustfmt (kind of) |
| homepage | |
| repository | https://github.com/smoelius/rustfmt_if_chain |
| max_upload_size | |
| id | 505812 |
| size | 61,915 |
A wrapper around rustfmt to format inside if_chain invocations
rustfmt_if_chain is not guaranteed to work on all Rust source files, but it should work on most of Clippy's source files.
cargo install rustfmt_if_chain
Usage: rustfmt_if_chain [ARGS]
Arguments ending with `.rs` are considered source files and are
formatted. All other arguments are forwarded to `rustfmt`, with
one exception.
The one argument not forwarded to `rustfmt` is
`--preformat-failure-is-warning`. If this option is passed and
`rustfmt` fails on an unmodified source file, a warning results
instead of an error.
Before
fn main() -> PhilosophicalResult<()> {
if_chain! { if let Some (it) = tree . falls_in (forest) ; if ! listeners . any (| one | one . around_to_hear (it)) ; if ! it . makes_a_sound () ; then { return Err (PhilosophicalError :: new ()) ; } }
Ok(())
}
After
fn main() -> PhilosophicalResult<()> {
if_chain! {
if let Some(it) = tree.falls_in(forest);
if !listeners.any(|one| one.around_to_hear(it));
if !it.makes_a_sound();
then {
return Err(PhilosophicalError::new());
}
}
Ok(())
}
rustfmt is run on the original source file to verify that it can be formatted.*if_chain invocations in the original source file are rewritten according to the following rules, where x is an identifier that does not appear elsewhere in the file:
if_chain! -> fn x() or |x| (depending on whether the invocation is an item or expression)if ... ; -> if ... { x; }then -> if xrustfmt is run on the file resulting from step 1.* Step 0 is not strictly necessary, but it helps to identify failures of step 2 caused by the limitations of step 1.
rustfmt_if_chain --check FILENAME does not work correctly. A workaround is to use rustfmt_if_chain FILENAME && git diff --exit-code.if_chain are not handled correctly: only the outer-most use is formatted.