fmt-rfcs icon indicating copy to clipboard operation
fmt-rfcs copied to clipboard

Allow chains to condense to remove some single line `)`

Open TheBlueMatt opened this issue 3 years ago • 0 comments

One common complaint I've seen (and made myself) coming to rust-guide-formatted code from many other languages is the vertical-ness of a lot of common rust code. This leads to less context when viewing diffs (or functions entirely in smaller editor windows) which can be a common source of bugs. There's good reason for the vertical layout in many cases, of course - it can substantially improve scan-ability of code layout, and like all things, there is a balance to be had.

One common pattern in Rust is a somewhat long line causing a closing ) to be on its own line. While this can improve scan-ability, it can generate some very awkward looking chains, especially with a trailing unwrap(), which is also exceedingly common.

For example, we have some code which is formatted as

        let data = Vec::<u8>::from_hex(
            "02020202020202020202 longish hex constant",
        )
        .unwrap();

according to https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/expressions.md#multi-line-elements

At the risk of starting a long discussion and flame-war on the topic of chains, I don't believe scan-ability of the code is substantially impacted (or arguably positively impacted by a change to allow the unwrap to appear on the same line as the ), as follows:

        let data = Vec::<u8>::from_hex(
            "02020202020202020202 longish hex constant",
        ).unwrap();

As a general principle, though its totally reasonable to have it be of the lowest priority, having a "provide more context in diffs and during editing by making things single-line where it does not at all impact scan-ability" principle would serve to avoid some bugs which, in my experience, are not entirely uncommon, though arguably scan-ability can sometimes avoid more/other bugs.

TheBlueMatt avatar May 26 '22 20:05 TheBlueMatt