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.
This commit is contained in:
Matthias Krüger 2021-10-15 07:44:45 +02:00 committed by GitHub
commit b74ae0487b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -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
}

View File

@ -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();
}
}