unsafe_code
documentation states:
This lint is intended to restrict the usage of
unsafe
blocks and other constructs (including, but not limited tono_mangle
,link_section
andexport_name
attributes) wrong usage of which causes undefined behavior.
However, defining/implementing an unsafe function/method without using any unsafe operation should not cause any undefined behavior. Could we relax the lint to authorize it?
Take the example of this trait:
trait WriteSlot {
fn slot_size(&self) -> usize;
/// # Safety
/// `slot` slice's length must be equal to the result of [`Self::slot_size`]
unsafe fn write_slot(self, slot: &mut [u8]);
}
Implementor of this trait could choose to use the precondition to do some unsafe optimization, e.g. core::ptr::copy_non_overlapping
without checking the size, but he can also choose to ignore the precondition and simply use safe code like <[u8]>::copy_from_slice
.
In this case, I find quite unfair to have the lint triggered while I'm just using an API safely.