Add SEMICOLON_IN_EXPRESSIONS_FROM_MACROS lint

cc #79813

This PR adds an allow-by-default future-compatibility lint
`SEMICOLON_IN_EXPRESSIONS_FROM_MACROS`. It fires when a trailing semicolon in a
macro body is ignored due to the macro being used in expression
position:

```rust
macro_rules! foo {
    () => {
        true; // WARN
    }
}

fn main() {
    let val = match true {
        true => false,
        _ => foo!()
    };
}
```

The lint takes its level from the macro call site, and
can be allowed for a particular macro by adding
`#[allow(semicolon_in_expressions_from_macros)]`.

The lint is set to warn for all internal rustc crates (when being built
by a stage1 compiler). After the next beta bump, we can enable
the lint for the bootstrap compiler as well.
This commit is contained in:
Aaron Hill 2020-12-07 18:55:00 -05:00
parent 0e190206e2
commit f9025512e7
No known key found for this signature in database
GPG key ID: B4087E510E98B164
10 changed files with 150 additions and 1 deletions

View file

@ -833,6 +833,7 @@ class RustBuild(object):
target_linker = self.get_toml("linker", build_section)
if target_linker is not None:
env["RUSTFLAGS"] += " -C linker=" + target_linker
# cfg(bootstrap): Add `-Wsemicolon_in_expressions_from_macros` after the next beta bump
env["RUSTFLAGS"] += " -Wrust_2018_idioms -Wunused_lifetimes"
if self.get_toml("deny-warnings", "rust") != "false":
env["RUSTFLAGS"] += " -Dwarnings"

View file

@ -1250,6 +1250,12 @@ impl<'a> Builder<'a> {
// some code doesn't go through this `rustc` wrapper.
lint_flags.push("-Wrust_2018_idioms");
lint_flags.push("-Wunused_lifetimes");
// cfg(bootstrap): unconditionally enable this warning after the next beta bump
// This is currently disabled for the stage1 libstd, since build scripts
// will end up using the bootstrap compiler (which doesn't yet support this lint)
if compiler.stage != 0 && mode != Mode::Std {
lint_flags.push("-Wsemicolon_in_expressions_from_macros");
}
if self.config.deny_warnings {
lint_flags.push("-Dwarnings");

View file

@ -0,0 +1,15 @@
// check-pass
// Ensure that trailing semicolons are allowed by default
macro_rules! foo {
() => {
true;
}
}
fn main() {
let val = match true {
true => false,
_ => foo!()
};
}

View file

@ -0,0 +1,30 @@
// check-pass
#![warn(semicolon_in_expressions_from_macros)]
#[allow(dead_code)]
macro_rules! foo {
($val:ident) => {
true; //~ WARN trailing
//~| WARN this was previously
//~| WARN trailing
//~| WARN this was previously
}
}
fn main() {
// This `allow` doesn't work
#[allow(semicolon_in_expressions_from_macros)]
let _ = {
foo!(first)
};
// This 'allow' doesn't work either
#[allow(semicolon_in_expressions_from_macros)]
let _ = foo!(second);
// But this 'allow' does
#[allow(semicolon_in_expressions_from_macros)]
fn inner() {
let _ = foo!(third);
}
}

View file

@ -0,0 +1,33 @@
warning: trailing semicolon in macro used in expression position
--> $DIR/semicolon-in-expressions-from-macros.rs:7:13
|
LL | true;
| ^
...
LL | foo!(first)
| ----------- in this macro invocation
|
note: the lint level is defined here
--> $DIR/semicolon-in-expressions-from-macros.rs:2:9
|
LL | #![warn(semicolon_in_expressions_from_macros)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79813 <https://github.com/rust-lang/rust/issues/79813>
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: trailing semicolon in macro used in expression position
--> $DIR/semicolon-in-expressions-from-macros.rs:7:13
|
LL | true;
| ^
...
LL | let _ = foo!(second);
| ------------ in this macro invocation
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79813 <https://github.com/rust-lang/rust/issues/79813>
= note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
warning: 2 warnings emitted