-
Notifications
You must be signed in to change notification settings - Fork 13.3k
unsafe_code
lint fires for unsafe fn
with safe body
#108926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@rustbot label +A-diagnostics |
What about unsafe traits? They are also forbidden by lint |
Yes, Both |
@rustbot claim |
I agree, the current behavior is quite confusing (but at least it is consistent between |
I agree there seems to be a general confusion regarding unsafe, so I decided to write down my mental model to help education in that regard (the main idea being to make the proof type explicit as it would be in dependent type programming languages). This general confusion could be decomposed in the following core confusions:
I am not recommending Rust to change anything. I am just trying to lift this general confusion regarding unsafe through educational materials. Whether Rust should change anything (and how) is up to the community as a whole. In particular, lifting the confusion should be enough to clarify issues like this one. Once the first core confusion is lifted it should be obvious that unsafe fns and unsafe traits are not unsafe code and should not lint. Only unsafe blocks and unsafe impls should lint. |
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=3f3fbdb3494c6770487823f284c303f9
With
forbid(unsafe_op_in_unsafe_fn)
, anunsafe fn
's body in principle no longer has an implicitunsafe
block. So,unsafe_code
should not trigger on anunsafe fn
alone: there is not necessarily any unsafe code inside!This has practical effect. Consider one crate defining interfaces with unsafe methods (e.g. representing an OS API). Another crate wants to create mock implementations of these interfaces that are 100% safe. While any crate consuming these mocks will necessarily contain
unsafe {...}
blocks, it is nice for the test support crate to declare it itself has only safe code.The text was updated successfully, but these errors were encountered: