The following pattern is common in diagnostic messages in rustc:
format!("found {} thing{}", x, if x != 1 { "s" } else { "" })
It would be good to extract all of these pluralisation checks into a function or macro:
format!("found {} thing{}", x, pluralise(x))
There's already one in src/librustc/ty/error.rs, but it's not used anywhere else, so we could move it to somewhere in src/librustc_errors, and replace the occurrences of this pattern with the macro.
This issue has been assigned to @V1shvesh via this comment.
I'd love to be assigned to this issue!
@rustbot assign @V1shvesh
So, I have moved the macro under src/librustc_errors in a file called pluralise.rs. I have also located all the occurrences of the pattern.
I am a bit new to Rust, so I still am learning about the module system. How shall I pull the macro into the scope of all the relevant files?
@V1shvesh: I'd put it in src/librustc_errors/lib.rs, so we don't have one new file for a single macro. Then you can use it from use syntax::errors;.
Thanks for pointing me in the right direction @varkor!
@V1shvesh: searching for { "s" } else { "" } and { "" } else { "s" } in rustc, I can see a few cases that weren't caught by https://github.com/rust-lang/rust/pull/64280, e.g. in resolve_lifetime.rs, diagnostics.rs, etc. Let's keep this issue open until they've all been replaced with the new macro.
I have open a pr to refactor the pluralisations remain, @Centril please have a look at it, thanks.
@varkor Oh, my bad. @glorv thanks for helping out!
Most helpful comment
@V1shvesh: searching for
{ "s" } else { "" }and{ "" } else { "s" }in rustc, I can see a few cases that weren't caught by https://github.com/rust-lang/rust/pull/64280, e.g. inresolve_lifetime.rs,diagnostics.rs, etc. Let's keep this issue open until they've all been replaced with the new macro.