-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add as_ptr_cast_mut
lint
#9572
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
Merged
Merged
Add as_ptr_cast_mut
lint
#9572
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_opt; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::{ | ||
mir::Mutability, | ||
ty::{self, Ty, TypeAndMut}, | ||
}; | ||
|
||
use super::AS_PTR_CAST_MUT; | ||
|
||
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>) { | ||
if let ty::RawPtr(ptrty @ TypeAndMut { mutbl: Mutability::Mut, .. }) = cast_to.kind() | ||
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Not, .. }) = | ||
cx.typeck_results().node_type(cast_expr.hir_id).kind() | ||
&& let ExprKind::MethodCall(method_name, receiver, [], _) = cast_expr.peel_blocks().kind | ||
&& method_name.ident.name == rustc_span::sym::as_ptr | ||
&& let Some(as_ptr_did) = cx.typeck_results().type_dependent_def_id(cast_expr.peel_blocks().hir_id) | ||
&& let as_ptr_sig = cx.tcx.fn_sig(as_ptr_did) | ||
&& let Some(first_param_ty) = as_ptr_sig.skip_binder().inputs().iter().next() | ||
&& let ty::Ref(_, _, Mutability::Not) = first_param_ty.kind() | ||
&& let Some(recv) = snippet_opt(cx, receiver.span) | ||
{ | ||
// `as_mut_ptr` might not exist | ||
let applicability = Applicability::MaybeIncorrect; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
AS_PTR_CAST_MUT, | ||
expr.span, | ||
&format!("casting the result of `as_ptr` to *{ptrty}"), | ||
"replace with", | ||
format!("{recv}.as_mut_ptr()"), | ||
applicability | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
### What it does | ||
Checks for the result of a `&self`-taking `as_ptr` being cast to a mutable pointer | ||
|
||
### Why is this bad? | ||
Since `as_ptr` takes a `&self`, the pointer won't have write permissions unless interior | ||
mutability is used, making it unlikely that having it as a mutable pointer is correct. | ||
|
||
### Example | ||
``` | ||
let string = String::with_capacity(1); | ||
let ptr = string.as_ptr() as *mut u8; | ||
unsafe { ptr.write(4) }; // UNDEFINED BEHAVIOUR | ||
``` | ||
Use instead: | ||
``` | ||
let mut string = String::with_capacity(1); | ||
let ptr = string.as_mut_ptr(); | ||
unsafe { ptr.write(4) }; | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::as_ptr_cast_mut)] | ||
#![allow(clippy::wrong_self_convention)] | ||
|
||
struct MutPtrWrapper(Vec<u8>); | ||
impl MutPtrWrapper { | ||
fn as_ptr(&mut self) -> *const u8 { | ||
self.0.as_mut_ptr() as *const u8 | ||
} | ||
} | ||
|
||
struct Covariant<T>(*const T); | ||
impl<T> Covariant<T> { | ||
fn as_ptr(self) -> *const T { | ||
self.0 | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut string = String::new(); | ||
let _ = string.as_ptr() as *mut u8; | ||
let _: *mut i8 = string.as_ptr() as *mut _; | ||
let _ = string.as_ptr() as *const i8; | ||
let _ = string.as_mut_ptr(); | ||
let _ = string.as_mut_ptr() as *mut u8; | ||
let _ = string.as_mut_ptr() as *const u8; | ||
|
||
let nn = std::ptr::NonNull::new(4 as *mut u8).unwrap(); | ||
let _ = nn.as_ptr() as *mut u8; | ||
|
||
let mut wrap = MutPtrWrapper(Vec::new()); | ||
let _ = wrap.as_ptr() as *mut u8; | ||
|
||
let mut local = 4; | ||
let ref_with_write_perm = Covariant(std::ptr::addr_of_mut!(local) as *const _); | ||
let _ = ref_with_write_perm.as_ptr() as *mut u8; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error: casting the result of `as_ptr` to *mut u8 | ||
--> $DIR/as_ptr_cast_mut.rs:21:13 | ||
| | ||
LL | let _ = string.as_ptr() as *mut u8; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `string.as_mut_ptr()` | ||
| | ||
= note: `-D clippy::as-ptr-cast-mut` implied by `-D warnings` | ||
|
||
error: casting the result of `as_ptr` to *mut i8 | ||
--> $DIR/as_ptr_cast_mut.rs:22:22 | ||
| | ||
LL | let _: *mut i8 = string.as_ptr() as *mut _; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `string.as_mut_ptr()` | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can check if it implements
as_mut_ptr
for the suggestion, but this can be a future improvement