Skip to content
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

Rustfmt removes trailing comma in argument list of macro input that looks like a function #6273

Closed
jdonszelmann opened this issue Aug 9, 2024 · 1 comment
Labels

Comments

@jdonszelmann
Copy link

Breaking example:

macro_rules! almost_function {
    (fn $name: ident($param: ident: $ty: ty,) $b: block) => {};
}

fn main() {
    almost_function!(
        fn x(something: usize,) {}
    );
}

note that this macro requires a trailing coma

I expected to see this happen: rustfmt doesn't remove the trailing comma in the input since it's required for this code to compile.
Instead, this happened: rustfmt reformats it to:

macro_rules! almost_function {
    (fn $name: ident($param: ident: $ty: ty,) $b: block) => {};
}

fn main() {
    almost_function!(
        fn x(something: usize) {}
    );
}

Note: alternatives that don't produce this problem:

macro_rules! almost_function {
    ($name: ident($param: ident: $ty: ty,) $b: block) => {};
}

fn main() {
    almost_function!(
        x (something: usize,) {}
    );
}
macro_rules! almost_function {
    (fn $name: ident $param: ident: $ty: ty, $b: block) => {};
}

fn main() {
    almost_function!(
        fn x something: usize, {}
    );
}
macro_rules! almost_function {
    (fn $name: ident [$param: ident: $ty: ty,] $b: block) => {};
}

fn main() {
    almost_function!(
        fn x [something: usize,] {}
    );
}

From those experiments, we can see that rustfmt only does it if it looks a lot like a function

Meta

rustfmt --version --verbose:

rustfmt 1.7.0-stable (25ef9e3 2024-04-09)
@ytmimi
Copy link
Contributor

ytmimi commented Aug 9, 2024

Similar to #6228. In this case it would be better to write the macro definition to make the comma optional (or not required at all).

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Aug 9, 2024
@ytmimi ytmimi added the a-macros label Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants