logoalt Hacker News

codedokodetoday at 1:06 PM3 repliesview on HN

Macros can secretly add "unsafe" blocks into the code?


Replies

kibwentoday at 1:38 PM

If you're paranoid, you can use the `forbid(unsafe_code)` attribute, which will produce a compiler error when any code in its scope attempts to use `unsafe`, which includes macro expansions.

EFLKumotoday at 1:15 PM

Yes. It assumes author of the macro guarantees the safety. Common cases are not adding unsafe{} and leaving this to user, relying on audit tools or [highlighters](https://lukaswirth.dev/posts/semantic-unsafe/), etc. However, it's indeed allowed to silently add unsafe blocks in macros. I'm not working on rust frequently btw, mistakes may exist.

mplanchardtoday at 1:14 PM

Macros are just text in, text out, so yep

show 1 reply