pub static EDITION_2024_EXPR_FRAGMENT_SPECIFIER: &Lint
Expand description
The edition_2024_expr_fragment_specifier
lint detects the use of
expr
fragments in macros during migration to the 2024 edition.
The expr
fragment specifier will accept more expressions in the 2024
edition. To maintain the behavior from the 2021 edition and earlier, use
the expr_2021
fragment specifier.
§Example
#![deny(edition_2024_expr_fragment_specifier)]
macro_rules! m {
($e:expr) => {
$e
}
}
fn main() {
m!(1);
}
{{produces}}
§Explanation
Rust editions allow the language to evolve without breaking backwards compatibility. This lint catches code that uses macro matcher fragment specifiers that have changed meaning in the 2024 edition. If you switch to the new edition without updating the code, your macros may behave differently.
In the 2024 edition, the expr
fragment specifier expr
will also
match const { ... }
blocks. This means if a macro had a pattern that
matched $e:expr
and another that matches const { $e: expr }
, for
example, that under the 2024 edition the first pattern would match while
in the 2021 and earlier editions the second pattern would match. To keep
the old behavior, use the expr_2021
fragment specifier.
This lint detects macros whose behavior might change due to the changing
meaning of the expr
fragment specifier. It is “allow” by default
because the code is perfectly valid in older editions. The cargo fix
tool with the --edition
flag will switch this lint to “warn” and
automatically apply the suggested fix from the compiler. This provides a
completely automated way to update old code for a new edition.
Using cargo fix --edition
with this lint will ensure that your code
retains the same behavior. This may not be the desired, as macro authors
often will want their macros to use the latest grammar for matching
expressions. Be sure to carefully review changes introduced by this lint
to ensure the macros implement the desired behavior.