Skip to content

Bad format for closure that have only one expression #1574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zonyitoo opened this issue May 22, 2017 · 1 comment
Closed

Bad format for closure that have only one expression #1574

zonyitoo opened this issue May 22, 2017 · 1 comment

Comments

@zonyitoo
Copy link

zonyitoo commented May 22, 2017

pub fn run(config: Config) -> io::Result<()> {
    let mut lp = try!(Core::new());
    let handle = lp.handle();

    let enable_udp = config.enable_udp;

    let context = Context::new(handle, config);
    Context::set(&context, move || if enable_udp {
        let tcp_fut = run_tcp();
        let udp_fut = run_udp();
        lp.run(tcp_fut.join(udp_fut).map(|_| ()))
    } else {
        let tcp_fut = run_tcp();
        lp.run(tcp_fut)
    })
}

As you can see, the closure passed to set function only has one if expression. And rustfmt won't allow me to add a block around it.

It would be better if it is formatted as

pub fn run(config: Config) -> io::Result<()> {
    let mut lp = try!(Core::new());
    let handle = lp.handle();

    let enable_udp = config.enable_udp;

    let context = Context::new(handle, config);
    Context::set(&context, move ||
        if enable_udp {
            let tcp_fut = run_tcp();
            let udp_fut = run_udp();
            lp.run(tcp_fut.join(udp_fut).map(|_| ()))
        } else {
            let tcp_fut = run_tcp();
            lp.run(tcp_fut)
        })
}

problem will also occurs for match and other expressions that has a block in it.

Maybe related to #1570 or #1504.

@nrc
Copy link
Member

nrc commented May 22, 2017

This is intentional. It might be worth bringing this up on rust-lang/style-team#61

@nrc nrc closed this as completed May 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants