Skip to content

Commit 66b2f9a

Browse files
committed
Add checking for export_name to unsafe_code lint
1 parent 06a0269 commit 66b2f9a

File tree

3 files changed

+71
-19
lines changed

3 files changed

+71
-19
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ impl EarlyLintPass for UnsafeCode {
283283
lint.build("declaration of a `no_mangle` function").emit();
284284
})
285285
}
286+
if attr::contains_name(&it.attrs, sym::export_name) {
287+
self.report_unsafe(cx, it.span, |lint| {
288+
lint.build("declaration of a function with `export_name`").emit();
289+
})
290+
}
286291
}
287292

288293
ast::ItemKind::Static(..) => {
@@ -291,6 +296,11 @@ impl EarlyLintPass for UnsafeCode {
291296
lint.build("declaration of a `no_mangle` static").emit();
292297
})
293298
}
299+
if attr::contains_name(&it.attrs, sym::export_name) {
300+
self.report_unsafe(cx, it.span, |lint| {
301+
lint.build("declaration of a static with `export_name`").emit();
302+
})
303+
}
294304
}
295305

296306
_ => {}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,27 @@ mod allowed_unsafe {
1313
unsafe trait AllowedUnsafe { }
1414
unsafe impl AllowedUnsafe for super::Bar {}
1515
#[no_mangle] fn allowed2() {}
16+
#[export_name = "foo"] fn allowed3() {}
1617
}
1718

1819
macro_rules! unsafe_in_macro {
1920
() => {{
2021
#[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function
2122
#[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static
23+
#[export_name = "bar"] fn bar() {}
24+
//~^ ERROR: declaration of a function with `export_name`
25+
#[export_name = "BAR"] static BAR: u32 = 5;
26+
//~^ ERROR: declaration of a static with `export_name`
2227
unsafe {} //~ ERROR: usage of an `unsafe` block
2328
}}
2429
}
2530

2631
#[no_mangle] fn foo() {} //~ ERROR: declaration of a `no_mangle` function
2732
#[no_mangle] static FOO: u32 = 5; //~ ERROR: declaration of a `no_mangle` static
2833

34+
#[export_name = "bar"] fn bar() {} //~ ERROR: declaration of a function with `export_name`
35+
#[export_name = "BAR"] static BAR: u32 = 5; //~ ERROR: declaration of a static with `export_name`
36+
2937
unsafe fn baz() {} //~ ERROR: declaration of an `unsafe` function
3038
unsafe trait Foo {} //~ ERROR: declaration of an `unsafe` trait
3139
unsafe impl Foo for Bar {} //~ ERROR: implementation of an `unsafe` trait
Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: declaration of a `no_mangle` function
2-
--> $DIR/lint-unsafe-code.rs:26:14
2+
--> $DIR/lint-unsafe-code.rs:31:14
33
|
44
LL | #[no_mangle] fn foo() {}
55
| ^^^^^^^^^^^
@@ -11,91 +11,103 @@ LL | #![deny(unsafe_code)]
1111
| ^^^^^^^^^^^
1212

1313
error: declaration of a `no_mangle` static
14-
--> $DIR/lint-unsafe-code.rs:27:14
14+
--> $DIR/lint-unsafe-code.rs:32:14
1515
|
1616
LL | #[no_mangle] static FOO: u32 = 5;
1717
| ^^^^^^^^^^^^^^^^^^^^
1818

19+
error: declaration of a function with `export_name`
20+
--> $DIR/lint-unsafe-code.rs:34:24
21+
|
22+
LL | #[export_name = "bar"] fn bar() {}
23+
| ^^^^^^^^^^^
24+
25+
error: declaration of a static with `export_name`
26+
--> $DIR/lint-unsafe-code.rs:35:24
27+
|
28+
LL | #[export_name = "BAR"] static BAR: u32 = 5;
29+
| ^^^^^^^^^^^^^^^^^^^^
30+
1931
error: declaration of an `unsafe` function
20-
--> $DIR/lint-unsafe-code.rs:29:1
32+
--> $DIR/lint-unsafe-code.rs:37:1
2133
|
2234
LL | unsafe fn baz() {}
2335
| ^^^^^^^^^^^^^^^^^^
2436

2537
error: declaration of an `unsafe` trait
26-
--> $DIR/lint-unsafe-code.rs:30:1
38+
--> $DIR/lint-unsafe-code.rs:38:1
2739
|
2840
LL | unsafe trait Foo {}
2941
| ^^^^^^^^^^^^^^^^^^^
3042

3143
error: implementation of an `unsafe` trait
32-
--> $DIR/lint-unsafe-code.rs:31:1
44+
--> $DIR/lint-unsafe-code.rs:39:1
3345
|
3446
LL | unsafe impl Foo for Bar {}
3547
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
3648

3749
error: declaration of an `unsafe` method
38-
--> $DIR/lint-unsafe-code.rs:34:5
50+
--> $DIR/lint-unsafe-code.rs:42:5
3951
|
4052
LL | unsafe fn baz(&self);
4153
| ^^^^^^^^^^^^^^^^^^^^^
4254

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

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

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

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

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

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

7991
error: implementation of an `unsafe` method
80-
--> $DIR/lint-unsafe-code.rs:77:5
92+
--> $DIR/lint-unsafe-code.rs:85:5
8193
|
8294
LL | unsafe fn provided(&self) {}
8395
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8496

8597
error: implementation of an `unsafe` method
86-
--> $DIR/lint-unsafe-code.rs:81:5
98+
--> $DIR/lint-unsafe-code.rs:89:5
8799
|
88100
LL | unsafe fn baz(&self) {}
89101
| ^^^^^^^^^^^^^^^^^^^^^^^
90102

91103
error: usage of an `unsafe` block
92-
--> $DIR/lint-unsafe-code.rs:92:5
104+
--> $DIR/lint-unsafe-code.rs:100:5
93105
|
94106
LL | unsafe {}
95107
| ^^^^^^^^^
96108

97109
error: declaration of a `no_mangle` function
98-
--> $DIR/lint-unsafe-code.rs:20:22
110+
--> $DIR/lint-unsafe-code.rs:21:22
99111
|
100112
LL | #[no_mangle] fn foo() {}
101113
| ^^^^^^^^^^^
@@ -106,7 +118,7 @@ LL | unsafe_in_macro!()
106118
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
107119

108120
error: declaration of a `no_mangle` static
109-
--> $DIR/lint-unsafe-code.rs:21:22
121+
--> $DIR/lint-unsafe-code.rs:22:22
110122
|
111123
LL | #[no_mangle] static FOO: u32 = 5;
112124
| ^^^^^^^^^^^^^^^^^^^^
@@ -116,8 +128,30 @@ LL | unsafe_in_macro!()
116128
|
117129
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
118130

131+
error: declaration of a function with `export_name`
132+
--> $DIR/lint-unsafe-code.rs:23:32
133+
|
134+
LL | #[export_name = "bar"] fn bar() {}
135+
| ^^^^^^^^^^^
136+
...
137+
LL | unsafe_in_macro!()
138+
| ------------------ in this macro invocation
139+
|
140+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
141+
142+
error: declaration of a static with `export_name`
143+
--> $DIR/lint-unsafe-code.rs:25:32
144+
|
145+
LL | #[export_name = "BAR"] static BAR: u32 = 5;
146+
| ^^^^^^^^^^^^^^^^^^^^
147+
...
148+
LL | unsafe_in_macro!()
149+
| ------------------ in this macro invocation
150+
|
151+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
152+
119153
error: usage of an `unsafe` block
120-
--> $DIR/lint-unsafe-code.rs:22:9
154+
--> $DIR/lint-unsafe-code.rs:27:9
121155
|
122156
LL | unsafe {}
123157
| ^^^^^^^^^
@@ -127,5 +161,5 @@ LL | unsafe_in_macro!()
127161
|
128162
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
129163

130-
error: aborting due to 18 previous errors
164+
error: aborting due to 22 previous errors
131165

0 commit comments

Comments
 (0)