about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2021-10-15 07:44:45 +0200
committerGitHub <noreply@github.com>2021-10-15 07:44:45 +0200
commitb74ae0487b81628f67f5ca66a12ebd636eb9cafe (patch)
treecbe3d8454b294af8a69fff8149db71b02da0fa01
parent36a1076d24697621a3bb67ef654b4eb79647aa54 (diff)
parentc76c620e4ec7d504fe42c9ca66bb55511fc9b23f (diff)
downloadrust-b74ae0487b81628f67f5ca66a12ebd636eb9cafe.tar.gz
rust-b74ae0487b81628f67f5ca66a12ebd636eb9cafe.zip
Rollup merge of #89821 - crlf0710:unsafe_code_lint_test, r=Mark-Simulacrum
Add a strange test for `unsafe_code` lint.

The current behavior is a little surprising to me. I'm not sure whether people would change it, but at least let me document the current behavior with a test.

I learnt about this from the [totally-speedy-transmute](https://docs.rs/totally-speedy-transmute) crate.

cc #10599 the original implementation pr.
-rw-r--r--src/test/ui/lint/unsafe_code/auxiliary/forge_unsafe_block.rs16
-rw-r--r--src/test/ui/lint/unsafe_code/forge_unsafe_block.rs16
2 files changed, 32 insertions, 0 deletions
diff --git a/src/test/ui/lint/unsafe_code/auxiliary/forge_unsafe_block.rs b/src/test/ui/lint/unsafe_code/auxiliary/forge_unsafe_block.rs
new file mode 100644
index 00000000000..26871c98dbe
--- /dev/null
+++ b/src/test/ui/lint/unsafe_code/auxiliary/forge_unsafe_block.rs
@@ -0,0 +1,16 @@
+// force-host
+// no-prefer-dynamic
+
+#![crate_type = "proc-macro"]
+
+extern crate proc_macro;
+
+use proc_macro::{Delimiter, Group, Ident, Span, TokenStream, TokenTree};
+
+#[proc_macro]
+pub fn forge_unsafe_block(input: TokenStream) -> TokenStream {
+    let mut output = TokenStream::new();
+    output.extend(Some(TokenTree::from(Ident::new("unsafe", Span::call_site()))));
+    output.extend(Some(TokenTree::from(Group::new(Delimiter::Brace, input))));
+    output
+}
diff --git a/src/test/ui/lint/unsafe_code/forge_unsafe_block.rs b/src/test/ui/lint/unsafe_code/forge_unsafe_block.rs
new file mode 100644
index 00000000000..a1bd7b41319
--- /dev/null
+++ b/src/test/ui/lint/unsafe_code/forge_unsafe_block.rs
@@ -0,0 +1,16 @@
+// check-pass
+// aux-build:forge_unsafe_block.rs
+
+#[macro_use]
+extern crate forge_unsafe_block;
+
+unsafe fn foo() {}
+
+#[forbid(unsafe_code)]
+fn main() {
+    // `forbid` doesn't work for non-user-provided unsafe blocks.
+    // see `UnsafeCode::check_expr`.
+    forge_unsafe_block! {
+        foo();
+    }
+}