Skip to content

Commit d38b788

Browse files
committed
Emit an error for invalid use of the #[no_sanitize] attribute
1 parent 4d48a6b commit d38b788

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

Diff for: compiler/rustc_passes/src/check_attr.rs

+6
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
125125
[sym::inline, ..] => self.check_inline(hir_id, attr, span, target),
126126
[sym::coverage, ..] => self.check_coverage(attr, span, target),
127127
[sym::optimize, ..] => self.check_optimize(hir_id, attr, target),
128+
[sym::no_sanitize, ..] => self.check_no_sanitize(hir_id, attr, span, target),
128129
[sym::non_exhaustive, ..] => self.check_non_exhaustive(hir_id, attr, span, target),
129130
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
130131
[sym::target_feature, ..] => {
@@ -450,6 +451,11 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
450451
}
451452
}
452453

454+
/// Checks that `#[no_sanitize(..)]` is applied to a function or method.
455+
fn check_no_sanitize(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
456+
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
457+
}
458+
453459
fn check_generic_attr(
454460
&self,
455461
hir_id: HirId,

Diff for: tests/ui/attributes/no-sanitize.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![feature(no_sanitize)]
2+
#![feature(stmt_expr_attributes)]
3+
#![deny(unused_attributes)]
4+
#![allow(dead_code)]
5+
6+
fn invalid() {
7+
#[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
8+
{
9+
1
10+
};
11+
}
12+
13+
#[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
14+
type InvalidTy = ();
15+
16+
#[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
17+
mod invalid_module {}
18+
19+
fn main() {
20+
let _ = #[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
21+
(|| 1);
22+
}
23+
24+
#[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
25+
struct F;
26+
27+
#[no_sanitize(memory)] //~ ERROR attribute should be applied to a function definition
28+
impl F {
29+
#[no_sanitize(memory)]
30+
fn valid(&self) {}
31+
}
32+
33+
#[no_sanitize(memory)]
34+
fn valid() {}

Diff for: tests/ui/attributes/no-sanitize.stderr

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
error: attribute should be applied to a function definition
2+
--> $DIR/no-sanitize.rs:7:5
3+
|
4+
LL | #[no_sanitize(memory)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
LL | / {
7+
LL | | 1
8+
LL | | };
9+
| |_____- not a function definition
10+
11+
error: attribute should be applied to a function definition
12+
--> $DIR/no-sanitize.rs:13:1
13+
|
14+
LL | #[no_sanitize(memory)]
15+
| ^^^^^^^^^^^^^^^^^^^^^^
16+
LL | type InvalidTy = ();
17+
| -------------------- not a function definition
18+
19+
error: attribute should be applied to a function definition
20+
--> $DIR/no-sanitize.rs:16:1
21+
|
22+
LL | #[no_sanitize(memory)]
23+
| ^^^^^^^^^^^^^^^^^^^^^^
24+
LL | mod invalid_module {}
25+
| --------------------- not a function definition
26+
27+
error: attribute should be applied to a function definition
28+
--> $DIR/no-sanitize.rs:20:13
29+
|
30+
LL | let _ = #[no_sanitize(memory)]
31+
| ^^^^^^^^^^^^^^^^^^^^^^
32+
LL | (|| 1);
33+
| ------ not a function definition
34+
35+
error: attribute should be applied to a function definition
36+
--> $DIR/no-sanitize.rs:24:1
37+
|
38+
LL | #[no_sanitize(memory)]
39+
| ^^^^^^^^^^^^^^^^^^^^^^
40+
LL | struct F;
41+
| --------- not a function definition
42+
43+
error: attribute should be applied to a function definition
44+
--> $DIR/no-sanitize.rs:27:1
45+
|
46+
LL | #[no_sanitize(memory)]
47+
| ^^^^^^^^^^^^^^^^^^^^^^
48+
LL | / impl F {
49+
LL | | #[no_sanitize(memory)]
50+
LL | | fn valid(&self) {}
51+
LL | | }
52+
| |_- not a function definition
53+
54+
error: aborting due to 6 previous errors
55+

0 commit comments

Comments
 (0)