If someone feels like writing a tool to manage this, adding the macro is as simple as a no-op:
// note: I'm simplifying the structure a little bit here
#[proc_macro_attribute]
fn safe(input: TokenStream) -> TokenStream {
let block: ExprUnsafe = parse(input)?;
block.attrs = block.attrs
.into_iter()
.filter(|attr| attr.path != parse_quote!(safe))
.collect();
block.into_token_stream()
}
Usage works so long as you put a ; after the unsafe block (which may require changing the macro though):
fn main(){
#[safe(true)]
unsafe {
println!("hi");
};
}
I could definitely see a tool that would scrape for unsafe blocks and their annotated #[safe] being used. A lot of high-quality codebases already put comments on unsafe blocks to explain what they’re doing (it’s good practice!) and there’s defnitely benefit to be had to structure it.
There’s also been suggestions to tie it to reviews. The possible metadata I see a #[safe] wanting, currently:
#[safe(
reason = "Description of what this unsafe is guaranteeing",
review = "link to some review of the code",
author = "person to blame for unsafety",
hash = "hash of the unsafe block contents, to prevent staling",
)]
But I still think as far as the “plugin” part of the macro goes, I think it definitely should no-op and let the external scraping tool do the real work.
Maybe @dpc would be interested after crev is production-ready, or otherwise the Secure Code WG could “sponsor” a tool for this.
(Unfortunately this is far from what I’m good at and I can’t really commit time to it, though it does seem both useful and interesting.)