Skip to content

Commit 06a0269

Browse files
committed
Add checking for no_mangle to unsafe_code lint
1 parent 85fbf49 commit 06a0269

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

compiler/rustc_lint/src/builtin.rs

+16
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ impl EarlyLintPass for UnsafeCode {
277277
})
278278
}
279279

280+
ast::ItemKind::Fn(..) => {
281+
if attr::contains_name(&it.attrs, sym::no_mangle) {
282+
self.report_unsafe(cx, it.span, |lint| {
283+
lint.build("declaration of a `no_mangle` function").emit();
284+
})
285+
}
286+
}
287+
288+
ast::ItemKind::Static(..) => {
289+
if attr::contains_name(&it.attrs, sym::no_mangle) {
290+
self.report_unsafe(cx, it.span, |lint| {
291+
lint.build("declaration of a `no_mangle` static").emit();
292+
})
293+
}
294+
}
295+
280296
_ => {}
281297
}
282298
}

src/test/ui/lint/lint-unsafe-code.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ mod allowed_unsafe {
1212
unsafe fn also_allowed() {}
1313
unsafe trait AllowedUnsafe { }
1414
unsafe impl AllowedUnsafe for super::Bar {}
15+
#[no_mangle] fn allowed2() {}
1516
}
1617

1718
macro_rules! unsafe_in_macro {
18-
() => {
19+
() => {{
20+
#[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function
21+
#[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static
1922
unsafe {} //~ ERROR: usage of an `unsafe` block
20-
}
23+
}}
2124
}
2225

26+
#[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function
27+
#[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static
28+
2329
unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
2430
unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
2531
unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait
+52-18
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,123 @@
1-
error: declaration of an `unsafe` function
2-
--> $DIR/lint-unsafe-code.rs:23:1
1+
error: declaration of a `no_mangle` function
2+
--> $DIR/lint-unsafe-code.rs:26:14
33
|
4-
LL | unsafe fn baz() {}
5-
| ^^^^^^^^^^^^^^^^^^
4+
LL | #[no_mangle] fn foo() {}
5+
| ^^^^^^^^^^^
66
|
77
note: the lint level is defined here
88
--> $DIR/lint-unsafe-code.rs:3:9
99
|
1010
LL | #![deny(unsafe_code)]
1111
| ^^^^^^^^^^^
1212

13+
error: declaration of a `no_mangle` static
14+
--> $DIR/lint-unsafe-code.rs:27:14
15+
|
16+
LL | #[no_mangle] static FOO: u32 = 5;
17+
| ^^^^^^^^^^^^^^^^^^^^
18+
19+
error: declaration of an `unsafe` function
20+
--> $DIR/lint-unsafe-code.rs:29:1
21+
|
22+
LL | unsafe fn baz() {}
23+
| ^^^^^^^^^^^^^^^^^^
24+
1325
error: declaration of an `unsafe` trait
14-
--> $DIR/lint-unsafe-code.rs:24:1
26+
--> $DIR/lint-unsafe-code.rs:30:1
1527
|
1628
LL | unsafe trait Foo {}
1729
| ^^^^^^^^^^^^^^^^^^^
1830

1931
error: implementation of an `unsafe` trait
20-
--> $DIR/lint-unsafe-code.rs:25:1
32+
--> $DIR/lint-unsafe-code.rs:31:1
2133
|
2234
LL | unsafe impl Foo for Bar {}
2335
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2436

2537
error: declaration of an `unsafe` method
26-
--> $DIR/lint-unsafe-code.rs:28:5
38+
--> $DIR/lint-unsafe-code.rs:34:5
2739
|
2840
LL | unsafe fn baz(&self);
2941
| ^^^^^^^^^^^^^^^^^^^^^
3042

3143
error: implementation of an `unsafe` method
32-
--> $DIR/lint-unsafe-code.rs:29:5
44+
--> $DIR/lint-unsafe-code.rs:35:5
3345
|
3446
LL | unsafe fn provided(&self) {}
3547
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3648

3749
error: implementation of an `unsafe` method
38-
--> $DIR/lint-unsafe-code.rs:30:5
50+
--> $DIR/lint-unsafe-code.rs:36:5
3951
|
4052
LL | unsafe fn provided_override(&self) {}
4153
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4254

4355
error: implementation of an `unsafe` method
44-
--> $DIR/lint-unsafe-code.rs:34:5
56+
--> $DIR/lint-unsafe-code.rs:40:5
4557
|
4658
LL | unsafe fn baz(&self) {}
4759
| ^^^^^^^^^^^^^^^^^^^^^^^
4860

4961
error: implementation of an `unsafe` method
50-
--> $DIR/lint-unsafe-code.rs:35:5
62+
--> $DIR/lint-unsafe-code.rs:41:5
5163
|
5264
LL | unsafe fn provided_override(&self) {}
5365
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5466

5567
error: implementation of an `unsafe` method
56-
--> $DIR/lint-unsafe-code.rs:54:5
68+
--> $DIR/lint-unsafe-code.rs:60:5
5769
|
5870
LL | unsafe fn provided_override(&self) {}
5971
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6072

6173
error: implementation of an `unsafe` method
62-
--> $DIR/lint-unsafe-code.rs:65:5
74+
--> $DIR/lint-unsafe-code.rs:71:5
6375
|
6476
LL | unsafe fn provided(&self) {}
6577
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6678

6779
error: implementation of an `unsafe` method
68-
--> $DIR/lint-unsafe-code.rs:71:5
80+
--> $DIR/lint-unsafe-code.rs:77:5
6981
|
7082
LL | unsafe fn provided(&self) {}
7183
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7284

7385
error: implementation of an `unsafe` method
74-
--> $DIR/lint-unsafe-code.rs:75:5
86+
--> $DIR/lint-unsafe-code.rs:81:5
7587
|
7688
LL | unsafe fn baz(&self) {}
7789
| ^^^^^^^^^^^^^^^^^^^^^^^
7890

7991
error: usage of an `unsafe` block
80-
--> $DIR/lint-unsafe-code.rs:86:5
92+
--> $DIR/lint-unsafe-code.rs:92:5
8193
|
8294
LL | unsafe {}
8395
| ^^^^^^^^^
8496

97+
error: declaration of a `no_mangle` function
98+
--> $DIR/lint-unsafe-code.rs:20:22
99+
|
100+
LL | #[no_mangle] fn foo() {}
101+
| ^^^^^^^^^^^
102+
...
103+
LL | unsafe_in_macro!()
104+
| ------------------ in this macro invocation
105+
|
106+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
107+
108+
error: declaration of a `no_mangle` static
109+
--> $DIR/lint-unsafe-code.rs:21:22
110+
|
111+
LL | #[no_mangle] static FOO: u32 = 5;
112+
| ^^^^^^^^^^^^^^^^^^^^
113+
...
114+
LL | unsafe_in_macro!()
115+
| ------------------ in this macro invocation
116+
|
117+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
118+
85119
error: usage of an `unsafe` block
86-
--> $DIR/lint-unsafe-code.rs:19:9
120+
--> $DIR/lint-unsafe-code.rs:22:9
87121
|
88122
LL | unsafe {}
89123
| ^^^^^^^^^
@@ -93,5 +127,5 @@ LL | unsafe_in_macro!()
93127
|
94128
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
95129

96-
error: aborting due to 14 previous errors
130+
error: aborting due to 18 previous errors
97131

0 commit comments

Comments
 (0)