Skip to content

No opinion on the semicolon after items defined within functions? #4222

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
iago-lito opened this issue Jun 1, 2020 · 2 comments · Fixed by #4232
Closed

No opinion on the semicolon after items defined within functions? #4222

iago-lito opened this issue Jun 1, 2020 · 2 comments · Fixed by #4232

Comments

@iago-lito
Copy link

iago-lito commented Jun 1, 2020

I have noticed that rustfmt leaves those 2 equivalent functions unchanged, although they are differently formatted:

fn first_function() -> ReturnType {
    struct inner_item {
        field: u32,
    }; // <- With a semicolon.
    last_expression
}

fn second_function() -> ReturnType {
    struct inner_item {
        field: u32,
    } // <- Without a semicolon.
    last_expression
}

Is this something that rustfmt should have an opinion about?

I would like the answer to be yes, because I would not have to choose myself whether to write this semicolon or not.

But I would understand that the answer be no, because ; is an additional (empty) statement that does not appear in the second_function, so the two functions are somewhat different. However, if this is how rustfmt stands, then it should maybe reformat the first snippet to

fn first_function() -> ReturnType {
    struct inner_item {
        field: u32,
    }
    ; // <- Make it clear that this is additional, empty statement.
    last_expression
}
@iago-lito
Copy link
Author

iago-lito commented Jun 2, 2020

FWIW, I've found that (postponed) RFC2479 is about this very semilon.

@ayazhafiz
Copy link
Contributor

Rustfmt already elides empty statements (per a comment on the RFC mentioned above, maybe this is actually unsafe?), so I think it is reasonable to elide the semicolon in this case too.

ayazhafiz added a commit to ayazhafiz/rustfmt that referenced this issue Jun 6, 2020
Per the Rust grammar, trailing semicolons on an item statement may be
optional (i.e. if the statement is a block). Keeping the semicolon in
such cases is unnecessary and may be confusing (does the semicolon
terminate the last statement, or is it an empty statement?), so just
drop it.

Closes rust-lang#4222
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants