-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
add new lint as_underscore_ptr
to check for as *{const,mut} _
#10567
Conversation
r? @Alexendoo (rustbot has picked a reviewer for you, use r? to override) |
1979448
to
033da3c
Compare
Does this also lint on mutability casts? Like: let ptr: *mut SomeVeryComplexType = ...;
return ptr as *const _; I feel like this lint can be very noisy in some cases, maybe |
It currently is pedantic because I was aware that lots of code uses these casts (not just in the mutability case). But yes it lints every time the target type of a cast is any pointer to |
e.g. like @Niki4tap brings up we could ignore when the T in fn f(x: &u8) {
// only the pointer/reference kind is changed, does not lint
let a: *const u8 = x as *const _;
let b: *mut u8 = a as *mut _;
// changes the type of the referent, would lint
let c: *const i8 = a as *const _;
} |
Linting on "a pointer cast that changes the type of the referent" for ptr->ptr is just the current lint that suggests Does clippy have a crater-like tool that I could use to better understand the cases where this fires and the lint's suggestion would be a downgrade? I expected the lint to be moderately common, hence an allow-by-default group, but also from my understanding, a significant number of people try to avoid this pattern as much as reasonable, because of how tricky the casts can be. The goal of this new lint is to find cases where the use of struct UwU;
impl UwU {
// intent: turn a `&UwU` into a `*const u8` that points to the same data
fn as_ptr(&self) -> *const u8 {
// ⚠️ `&self` is a `&&UwU`, so this turns a double pointer into a single pointer
// ⚠️ This pointer is a dangling pointer to a local
&self as *const _ as *const u8
}
} ⬆️ (turns a double pointer into a single pointer because the type of fn uwu(data: &[u64]) -> *const u64 { // changing the return type is dangerous
data.as_ptr() as *const _
} ⬆️ (the pointer type could be easily changed by inference) unsafe fn read_data(data: *const u32) { // 1. if this signature changes
let _ = *data;
}
let arr = [1_u32, 2, 3, 4];
unsafe {
let ptr = arr.as_ptr().add(2) as *const _; // 2. this inference can change
/*
some more code
*/
read_data(ptr); // 3. because of this call
} ⬆️ (inference for a local is determined by how it gets used later) Cases where the type is not obvious and cases where the type is obvious look really similar, and I'm not sure if clippy wants the complexity of picking out specific patterns for this lint, if the information such as "what controls this inference" is even available at all. I would like to improve the lint so that people can use it via |
On your point of trying to exclude "the referent is unchanged", in the example I provide in the lint (and the first example in my previous comment) the problematic cast is "reference to pointer that does not change the pointed to type", |
Thanks for the examples! Yeah at the very least this is a useful I saw the first example in the test file that didn't contain the For the other two, if the lint didn't fire because the type behind the pointer is unchanged it would at least serve as a guard ensuring it stays that way |
A route we could go is to land this as-is in |
The use of UwU instead of the boring |
I think that "is more than indirection" could be a useful heuristic for showing an additional I will also update the UI test file to contain exactly the same code and comments as the example, since that's more clear. |
IMHO - if you divide case - casting to nested-pointer (like |
I implemented the changes to this lint to show an extra note for some especially suspicious cases, and moved the lint down to |
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.
Thanks for that, just a couple things then it'd good to merge. Sorry for the delay!
What do I do about that CI failure? It says that the |
4247354
to
58f6680
Compare
58f6680
to
ecabab3
Compare
☔ The latest upstream changes (presumably #10416) made this pull request unmergeable. Please resolve the merge conflicts. |
I brought this up in the last meeting re: it being |
This probably could go in
suspicious
, but I was unsure where exactly to put it. It's not necessarily wrong code on its own, but can be problematic in certain situations.changelog: [
as_underscore_ptr
]: a new lint to detect use ofas *const _
oras *mut _
which can be the cause of subtle raw pointer bugs.