diff options
| author | Wim Looman <git@nemo157.com> | 2020-05-14 17:31:06 +0200 |
|---|---|---|
| committer | Wim Looman <git@nemo157.com> | 2020-08-30 20:16:22 +0200 |
| commit | 06a0269c110975d72a7312aab593abbb66c47f27 (patch) | |
| tree | 22630233ec479f6450935bee159664bd373445e9 | |
| parent | 85fbf49ce0e2274d0acf798f6e703747674feec3 (diff) | |
| download | rust-06a0269c110975d72a7312aab593abbb66c47f27.tar.gz rust-06a0269c110975d72a7312aab593abbb66c47f27.zip | |
Add checking for no_mangle to unsafe_code lint
| -rw-r--r-- | compiler/rustc_lint/src/builtin.rs | 16 | ||||
| -rw-r--r-- | src/test/ui/lint/lint-unsafe-code.rs | 10 | ||||
| -rw-r--r-- | src/test/ui/lint/lint-unsafe-code.stderr | 70 |
3 files changed, 76 insertions, 20 deletions
diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index ea624b9ed30..ed6dab2f0a6 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -277,6 +277,22 @@ impl EarlyLintPass for UnsafeCode { }) } + ast::ItemKind::Fn(..) => { + if attr::contains_name(&it.attrs, sym::no_mangle) { + self.report_unsafe(cx, it.span, |lint| { + lint.build("declaration of a `no_mangle` function").emit(); + }) + } + } + + ast::ItemKind::Static(..) => { + if attr::contains_name(&it.attrs, sym::no_mangle) { + self.report_unsafe(cx, it.span, |lint| { + lint.build("declaration of a `no_mangle` static").emit(); + }) + } + } + _ => {} } } diff --git a/src/test/ui/lint/lint-unsafe-code.rs b/src/test/ui/lint/lint-unsafe-code.rs index 735f33f601f..79c44c57fc9 100644 --- a/src/test/ui/lint/lint-unsafe-code.rs +++ b/src/test/ui/lint/lint-unsafe-code.rs @@ -12,14 +12,20 @@ mod allowed_unsafe { unsafe fn also_allowed() {} unsafe trait AllowedUnsafe { } unsafe impl AllowedUnsafe for super::Bar {} + #[no_mangle] fn allowed2() {} } macro_rules! unsafe_in_macro { - () => { + () => {{ + #[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function + #[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static unsafe {} //~ ERROR: usage of an `unsafe` block - } + }} } +#[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function +#[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static + unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait diff --git a/src/test/ui/lint/lint-unsafe-code.stderr b/src/test/ui/lint/lint-unsafe-code.stderr index 0b2b9fab392..a674bdf0748 100644 --- a/src/test/ui/lint/lint-unsafe-code.stderr +++ b/src/test/ui/lint/lint-unsafe-code.stderr @@ -1,8 +1,8 @@ -error: declaration of an `unsafe` function - --> $DIR/lint-unsafe-code.rs:23:1 +error: declaration of a `no_mangle` function + --> $DIR/lint-unsafe-code.rs:26:14 | -LL | unsafe fn baz() {} - | ^^^^^^^^^^^^^^^^^^ +LL | #[no_mangle] fn foo() {} + | ^^^^^^^^^^^ | note: the lint level is defined here --> $DIR/lint-unsafe-code.rs:3:9 @@ -10,80 +10,114 @@ note: the lint level is defined here LL | #![deny(unsafe_code)] | ^^^^^^^^^^^ +error: declaration of a `no_mangle` static + --> $DIR/lint-unsafe-code.rs:27:14 + | +LL | #[no_mangle] static FOO: u32 = 5; + | ^^^^^^^^^^^^^^^^^^^^ + +error: declaration of an `unsafe` function + --> $DIR/lint-unsafe-code.rs:29:1 + | +LL | unsafe fn baz() {} + | ^^^^^^^^^^^^^^^^^^ + error: declaration of an `unsafe` trait - --> $DIR/lint-unsafe-code.rs:24:1 + --> $DIR/lint-unsafe-code.rs:30:1 | LL | unsafe trait Foo {} | ^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` trait - --> $DIR/lint-unsafe-code.rs:25:1 + --> $DIR/lint-unsafe-code.rs:31:1 | LL | unsafe impl Foo for Bar {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: declaration of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:28:5 + --> $DIR/lint-unsafe-code.rs:34:5 | LL | unsafe fn baz(&self); | ^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:29:5 + --> $DIR/lint-unsafe-code.rs:35:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:30:5 + --> $DIR/lint-unsafe-code.rs:36:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:34:5 + --> $DIR/lint-unsafe-code.rs:40:5 | LL | unsafe fn baz(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:35:5 + --> $DIR/lint-unsafe-code.rs:41:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:54:5 + --> $DIR/lint-unsafe-code.rs:60:5 | LL | unsafe fn provided_override(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:65:5 + --> $DIR/lint-unsafe-code.rs:71:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:71:5 + --> $DIR/lint-unsafe-code.rs:77:5 | LL | unsafe fn provided(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: implementation of an `unsafe` method - --> $DIR/lint-unsafe-code.rs:75:5 + --> $DIR/lint-unsafe-code.rs:81:5 | LL | unsafe fn baz(&self) {} | ^^^^^^^^^^^^^^^^^^^^^^^ error: usage of an `unsafe` block - --> $DIR/lint-unsafe-code.rs:86:5 + --> $DIR/lint-unsafe-code.rs:92:5 | LL | unsafe {} | ^^^^^^^^^ +error: declaration of a `no_mangle` function + --> $DIR/lint-unsafe-code.rs:20:22 + | +LL | #[no_mangle] fn foo() {} + | ^^^^^^^^^^^ +... +LL | unsafe_in_macro!() + | ------------------ in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: declaration of a `no_mangle` static + --> $DIR/lint-unsafe-code.rs:21:22 + | +LL | #[no_mangle] static FOO: u32 = 5; + | ^^^^^^^^^^^^^^^^^^^^ +... +LL | unsafe_in_macro!() + | ------------------ in this macro invocation + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + error: usage of an `unsafe` block - --> $DIR/lint-unsafe-code.rs:19:9 + --> $DIR/lint-unsafe-code.rs:22:9 | LL | unsafe {} | ^^^^^^^^^ @@ -93,5 +127,5 @@ LL | unsafe_in_macro!() | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 14 previous errors +error: aborting due to 18 previous errors |
