-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Assignment inside format_args! is ignored #45256
Comments
What's odd is that println!("{x} {} {} {}", 1, x = 2, y= 3);
// => 2 1 2 3 |
The The bug here is that |
What @kennytm and @sinku said is correct, but I’m not sure it (the behaviour of how named arguments are considered in context of positional arguments) can be considered a bug anymore. At least I can easily see somebody using this, intentionally or not, even if it wasn’t documented to have specifically this behaviour. Therefore nominating it for T-lang (since format_args! is a part of language) to decide what the intended behaviour is. |
From Positional parameters:
Which suggests this is working as intended. If anything I'd say there should be a warning if you name a parameter but never reference it by its name. |
@ollie27 Isn't that talking only about positional parameters? The behavior in question have been introduced in Rust 1.12. I speculate that #33642 (which seems to have added internal translation of named args into positional args) unintentionally did it. Example: fn main() {
println!("{}", x = 1);
} Rust 1.11.0:
Rust 1.12.0 (compiles fine, and the program prints "1") |
Consensus in the lang team meeting is that we should reintroduce a lint for "named argument never used". And if it's just a lint, we don't need a full crater run. |
#98580 will lint against this as warn-by-default. |
#98580 has been merged -- should this issue be closed? |
Assignments inside
format_args!
are ignored. Instead only the right-hand side of the expression is used.I tried this code:
or a similar one, using directly
format_args
:I expected to see this:
Instead the output was:
It looks like the the assignment
x = 3
is ignored and is instead treated as if it was just3
.I could only replicate this with
format_args!
and macros that use it. Other macros, likeassert_eq!
, work as expected.Meta
rustc --version --verbose
:(also tried on
rustc 1.20.0-stable
andrustc 1.21.0-stable
)The text was updated successfully, but these errors were encountered: