Should short function bodies be allowed on a single line?

It appears that cargo fmt wants to always split the body of a function across 3+ lines, no matter how trivial it is, whereas, I would prefer to have them on the same line as the function signature if it is short.

For example, suppose I define a trait as well as a dummy impl like this

trait Output {
    fn new() -> Self;
    fn append(&self, u8) -> Self;
    fn concat(&self, &Self) -> Self;
}
impl Output for () {
    fn new() -> Self {()}
    fn append(&self, c: u8) -> Self {()}
    fn concat(&self, rhs: &Self) -> Self {()}
}

rustfmt wants to change it to this, which IMO looks pretty silly and takes up unnecessary space, making it harder to read.

trait Output {
    fn new() -> Self;
    fn append(&self, u8) -> Self;
    fn concat(&self, &Self) -> Self;
}
impl Output for () {
    fn new() -> Self {
        ()
    }
    fn append(&self, c: u8) -> Self {
        ()
    }
    fn concat(&self, rhs: &Self) -> Self {
        ()
    }
}
2 Likes

As I understand, this is already implemented in rustfmt as fn_single_line configuration, and it’s a matter of deciding the default.

1 Like

Maybe the default should be a heuristic? Something like "Allow a single line if it wouldn't push the code over 80 columns wide, and if the body of the function is a single sufficiently simple expression", where "sufficiently simple" is defined to do the Right Thing in the majority of cases.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.