You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of #9700 - andreubotella:from-raw-with-void-non-box, r=flip1995
Update `from_raw_with_void_ptr` to support types other than `Box`
This PR updates the `from_raw_with_void_ptr` lint, which covered
`Box::from_raw`, to also cover the `from_raw` static method of the
`Rc`, `Arc`, `alloc::rc::Weak` and `alloc::sync::Weak` types.
It also improves the description and error messages of this lint.
---
changelog: [`from_raw_with_void_ptr`]: Now works with the `Rc`, `Arc`, `alloc::rc::Weak` and `alloc::sync::Weak` types.
/// meaning that if you pass a *mut c_void you will get a Box<c_void>.
21
-
/// Per the safety requirements in the documentation, for this to be safe,
22
-
/// c_void would need to have the same memory layout as the original type, which is often not the case.
17
+
/// When dealing with `c_void` raw pointers in FFI, it is easy to run into the pitfall of calling `from_raw` with the `c_void` pointer.
18
+
/// The type signature of `Box::from_raw` is `fn from_raw(raw: *mut T) -> Box<T>`, so if you pass a `*mut c_void` you will get a `Box<c_void>` (and similarly for `Rc`, `Arc` and `Weak`).
19
+
/// For this to be safe, `c_void` would need to have the same memory layout as the original type, which is often not the case.
&& let arg_kind = cx.typeck_results().expr_ty(arg).kind()
52
48
&& letRawPtr(TypeAndMut{ ty, .. }) = arg_kind
53
49
&& is_c_void(cx,*ty){
54
-
span_lint_and_help(cx,FROM_RAW_WITH_VOID_PTR, expr.span,"creating a `Box` from a raw void pointer",Some(arg.span),"cast this to a pointer of the actual type");
50
+
let msg = format!("creating a `{type_str}` from a void raw pointer");
51
+
span_lint_and_help(cx,FROM_RAW_WITH_VOID_PTR, expr.span,&msg,Some(arg.span),"cast this to a pointer of the appropriate type");
52
+
}
53
+
}
54
+
}
55
+
56
+
/// Checks whether a `DefId` matches `Box`, `Rc`, `Arc`, or one of the `Weak` types.
57
+
/// Returns a static string slice with the name of the type, if one was found.
0 commit comments