Now that we have the #[diagnostic::] attribute namespace, I think it'd be helpful to add a #[diagnostic::unsafe_macro] hint. This would encourage tooling to, on a best-effort basis only, provide assists potentially including but not limited to:
- When highlighting uses of
unsafeconstructs (e.g.unsafe impl,unsafe fncalls, etc.), highlight uses of the annotated macro. Potential users: IDEs, rustdoc, cargo-geiger[1] - When expanding an annotated macro, warn if it's not syntactically contained in another
unsafeconstruction and is an expression/statement position expansion (thus could be so contained). Potential users: rustc, clippy
As a #[diagnostic], this is entirely best-effort; it should never cause hard errors (deny-by-default is permissable at the tool author's digression) and is not intended to be a substitute for actual unsafe macros â whenever possible such macros should still require the user to write unsafe[2] and avoid violating conceptual unsafe hygiene[3] â but merely a more lightweight intermediate assistance for improving things incrementally.
Likely limited to seeing decls and not calls, since it isn't doing any name resolution. âŠī¸
Expression position macros can ensure they expand to include a call to some
unsafe fnnot in an expansion-providedunsafeblock. Item position macros aren't so lucky and should ideally include e.g.unsafe implin their input. (Alternatively, if no deliberately usable item names are defined, onlyimpls, make it an expression position macro and require the caller to call asconst _: () = unsafe { m!() };... which is likely just more annoying than pseudo-beneficial.) Attribute macros have some options, âŠī¸If a macro uses
unsafeblocks internally, avoid expanding captured expressions inside of the block, as then the caller is permitted to dounsafethings covered by yourunsafeblock, this without writingunsafethemselves. This can be desirable in select cases â e.g. a privateffi!macro in an FFI adapter crate could be considered a validunsafeintroducer, as FFI is an "obviously"unsafebehavior â but generally isn't. âŠī¸