Rustfmt: Large `match` expression not formatted + "Can't set some features ..." errors

Created on 26 Nov 2017  路  7Comments  路  Source: rust-lang/rustfmt

I have a file with a large match expression. When I run rustfmt on it I see two things

First it prints these warnings:

$ rustfmt src/msg_handler.rs
Warning: can't set some features as unstable features are only available in nightly channel.
Warning: can't set some features as unstable features are only available in nightly channel.
Warning: can't set some features as unstable features are only available in nightly channel.
Warning: can't set some features as unstable features are only available in nightly channel.

I have no idea what are these saying. Which features are not set? Why does it need to set features? I'm using nightly so I don't understand why it's complaining about not using nightly channel.

Second, it doesn't format the file match expression, skipping 367 lines of code.

This used to work until I updated rustfmt today. I tried both rustfmt-nightly package and cargo install --git.

Note that this match expression doesn't have too deeply nested expressions or statements, it just has too many alternatives. There's really nothing wrong with this expression and it should be formatted by rustfmt.

(Somewhat related is we format machine-generated code with rustfmt so if for some reason by default rustfmt won't format large expressions it should at least a flag to disable such size checks)

a-comments poor-formatting

Most helpful comment

I think we should be not be checking CFG_RELEASE_CHANNEL, or perhaps we should be checking it at compile time instead of runtime.

All 7 comments

I managed to find a smaller match expression that rustfmt ignores:

pub fn handle_msg(conn: &mut Conn, tui: &mut TUI, logger: &mut Logger, msg: Msg, ts: Timestamp) {
    let pfx = msg.pfx;
match msg.cmd {
    Cmd::PRIVMSG { target, msg } | Cmd::NOTICE { target, msg } =>
        privmsg(conn, tui, logger, pfx, target, msg, ts),
    Cmd::JOIN { chan } =>
        join(conn, tui, logger, pfx, chan),
    Cmd::PART { chan, .. } =>
        part(conn, tui, logger, pfx, chan),
    Cmd::QUIT { .. } =>
        quit(conn, tui, logger, pfx),
    Cmd::NICK { nick: nick_ } =>
        nick(conn, tui, logger, pfx, nick_),
    Cmd::PING { .. } | Cmd::PONG { .. } =>
        // ignore
        {}
    Cmd::ERROR { msg } =>
        error(conn, tui, msg),
    Cmd::Reply { num, params } =>
        reply(conn, tui, logger, pfx, num, params),
    Cmd::Other { cmd, params } =>
        other(conn, tui, logger, pfx, cmd, params),
}
}

rustfmt only indents the match msg.cmd line but doesn't touch the rest.

@osa1 w.r.t. match expression getting skipped, the comment // ignore is the root of the evil. There is somewhat a related issue (#1087).

This comment is there for a long time and IIRC rustfmt used to deal with it somehow. This started happening recently, two weeks ago everything was fine.

Same problem:

$ rustfmt src/main.rs                    
Warning: can't set some features as unstable features are only available in nightly channel.
Warning: can't set some features as unstable features are only available in nightly channel.
Warning: can't set some features as unstable features are only available in nightly channel.
Warning: can't set some features as unstable features are only available in nightly channel.
Warning: can't set some features as unstable features are only available in nightly channel.
Warning: can't set some features as unstable features are only available in nightly channel.

src/main.rs:

fn main() {
    println!("Hello, world!");
}

BTW,

fn main() {
    println!("{:?}", std::env::var("CFG_RELEASE_CHANNEL"));
}

gives

Err(NotPresent)

I had to manually set CFG_RELEASE_CHANNEL to work around this:

export CFG_RELEASE_CHANNEL=nightly

Obviously not a decent solution :)

I think we should be not be checking CFG_RELEASE_CHANNEL, or perhaps we should be checking it at compile time instead of runtime.

The CFG_RELEASE_CHANNEL part of this should be fixed now, so remaining is the comment in match issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

0x7CFE picture 0x7CFE  路  5Comments

alatiera picture alatiera  路  4Comments

gnzlbg picture gnzlbg  路  3Comments

Diggsey picture Diggsey  路  4Comments

thomaseizinger picture thomaseizinger  路  3Comments