第一次发帖 有点儿整不会了 最近升级rust
发现很多unsafe fn
中的unsafe
代码也需要加unsafe
了吗?那unsafe fn
还有什么意义?不太理解 请教各位大神……
/// # Safety
pub unsafe fn copy_to_clipboard<T: ToString>(e: T) -> bool {
let pastboard = icrate::AppKit::NSPasteboard::generalPasteboard();
pastboard.clearContents();
let string = icrate::Foundation::NSString::from_str(e.to_string().as_str());
pastboard.setString_forType(&string, icrate::AppKit::NSPasteboardTypeString)
}
warning: call to unsafe function icrate::AppKit::NSPasteboard::generalPasteboard
is unsafe and requires unsafe block (error E0133)
--> ns_helper/base.rs:64:21
|
64 | let pastboard = icrate::AppKit::NSPasteboard::generalPasteboard();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
|
= note: for more information, see issue #71668 https://github.com/rust-lang/rust/issues/71668
= note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
评论区
写评论OK 找到了!
https://doc.rust-lang.org/stable/nightly-rustc/rustc_lint/builtin/static.UNSAFE_OP_IN_UNSAFE_FN.html
Static rustc_lint::builtin::UNSAFE_OP_IN_UNSAFE_FNCopy item path source · [−] pub static UNSAFE_OP_IN_UNSAFE_FN: &'static Lint The unsafe_op_in_unsafe_fn lint detects unsafe operations in unsafe functions without an explicit unsafe block.
Example ⓘ
Explanation Currently, an unsafe fn allows any unsafe operation within its body. However, this can increase the surface area of code that needs to be scrutinized for proper behavior. The unsafe block provides a convenient way to make it clear exactly which parts of the code are performing unsafe operations. In the future, it is desired to change it so that unsafe operations cannot be performed in an unsafe fn without an unsafe block.
The fix to this is to wrap the unsafe code in an unsafe block.
This lint is “allow” by default on editions up to 2021, from 2024 it is “warn” by default; the plan for increasing severity further is still being considered. See RFC #2585 and issue #71668 for more details.
多谢指教! 好比俄罗斯套娃 是吧 哈哈。我发现可能是因为我
Cargo.toml
开了cargo-features = ["edition2024"]
跟着那个issue链接能找到相关的RFC。我是这么想的:函数的 unsafe 标记是(实现者)将安全性保证委托给函数的调用者,而 unsafe block 是(编译器)将安全性保证交给写其中代码的人(实现者)。