diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-11-04 18:52:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-04 18:52:29 +0100 |
| commit | 347c478d886acc7e1b94af83f7fa882f73e809b7 (patch) | |
| tree | 9d036093f1f5c3db60c9c3c10ed2c20fe210b895 /src/test/ui | |
| parent | 82d7de837c83063af323c6c3c003bf562f5a004e (diff) | |
| parent | 28819cbb7e7ae05dc83e285ca936ac16876fd701 (diff) | |
Rollup merge of #103964 - clubby789:lint-enclosing-unsafe, r=compiler-errors
Give a specific lint for unsafety not being inherited
In cases like
```rs
static mut FOO: u64 = 0;
fn main() {
unsafe {static BAR: u64 = FOO;}
}
```
and
```rs
fn foo() {
unsafe {
fn bar() {
unsafe_call();
}
}
}
```
Specifically inform the user that the unsafety is not inherited for the seperate enclosing items
Fixes #94077
r? compiler-errors
`@rustbot` label +A-diagnostics
Diffstat (limited to 'src/test/ui')
| -rw-r--r-- | src/test/ui/unsafe/unsafe-not-inherited.rs | 26 | ||||
| -rw-r--r-- | src/test/ui/unsafe/unsafe-not-inherited.stderr | 24 |
2 files changed, 50 insertions, 0 deletions
diff --git a/src/test/ui/unsafe/unsafe-not-inherited.rs b/src/test/ui/unsafe/unsafe-not-inherited.rs new file mode 100644 index 00000000000..6d797caa0f9 --- /dev/null +++ b/src/test/ui/unsafe/unsafe-not-inherited.rs @@ -0,0 +1,26 @@ +#![allow(unused, dead_code)] + +static mut FOO: u64 = 0; + +fn static_mod() { + unsafe {static BAR: u64 = FOO;} + //~^ ERROR: use of mutable static is unsafe + //~| NOTE: use of mutable static + //~| NOTE: mutable statics can be mutated by multiple threads + //~| NOTE: items do not inherit unsafety +} + +unsafe fn unsafe_call() {} +fn foo() { + unsafe { + //~^ NOTE: items do not inherit unsafety + fn bar() { + unsafe_call(); + //~^ ERROR: call to unsafe function + //~| NOTE: call to unsafe function + //~| NOTE: consult the function's documentation + } + } +} + +fn main() {} diff --git a/src/test/ui/unsafe/unsafe-not-inherited.stderr b/src/test/ui/unsafe/unsafe-not-inherited.stderr new file mode 100644 index 00000000000..3bc5ca5c9d1 --- /dev/null +++ b/src/test/ui/unsafe/unsafe-not-inherited.stderr @@ -0,0 +1,24 @@ +error[E0133]: use of mutable static is unsafe and requires unsafe function or block + --> $DIR/unsafe-not-inherited.rs:6:31 + | +LL | unsafe {static BAR: u64 = FOO;} + | ------ ^^^ use of mutable static + | | + | items do not inherit unsafety from separate enclosing items + | + = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior + +error[E0133]: call to unsafe function is unsafe and requires unsafe function or block + --> $DIR/unsafe-not-inherited.rs:18:13 + | +LL | unsafe { + | ------ items do not inherit unsafety from separate enclosing items +... +LL | unsafe_call(); + | ^^^^^^^^^^^^^ call to unsafe function + | + = note: consult the function's documentation for information on how to avoid undefined behavior + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0133`. |
