-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add a new lint ptr_as_ptr
#6542
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
Changes from all commits
Commits
Show all changes
2 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
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,50 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::ptr_as_ptr)] | ||
#![feature(custom_inner_attributes)] | ||
|
||
fn main() { | ||
flip1995 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
let _ = ptr.cast::<i32>(); | ||
let _ = mut_ptr.cast::<i32>(); | ||
|
||
// Make sure the lint can handle the difference in their operator precedences. | ||
unsafe { | ||
let ptr_ptr: *const *const u32 = &ptr; | ||
let _ = (*ptr_ptr).cast::<i32>(); | ||
} | ||
|
||
// Changes in mutability. Do not lint this. | ||
let _ = ptr as *mut i32; | ||
let _ = mut_ptr as *const i32; | ||
|
||
// `pointer::cast` cannot perform unsized coercions unlike `as`. Do not lint this. | ||
let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4]; | ||
let _ = ptr_of_array as *const [u32]; | ||
let _ = ptr_of_array as *const dyn std::fmt::Debug; | ||
|
||
// Ensure the lint doesn't produce unnecessary turbofish for inferred types. | ||
let _: *const i32 = ptr.cast(); | ||
let _: *mut i32 = mut_ptr.cast(); | ||
} | ||
|
||
fn _msrv_1_37() { | ||
#![clippy::msrv = "1.37"] | ||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
// `pointer::cast` was stabilized in 1.38. Do not lint this | ||
let _ = ptr as *const i32; | ||
let _ = mut_ptr as *mut i32; | ||
} | ||
|
||
fn _msrv_1_38() { | ||
#![clippy::msrv = "1.38"] | ||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
let _ = ptr.cast::<i32>(); | ||
let _ = mut_ptr.cast::<i32>(); | ||
} |
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,50 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::ptr_as_ptr)] | ||
#![feature(custom_inner_attributes)] | ||
|
||
fn main() { | ||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
let _ = ptr as *const i32; | ||
let _ = mut_ptr as *mut i32; | ||
|
||
// Make sure the lint can handle the difference in their operator precedences. | ||
unsafe { | ||
let ptr_ptr: *const *const u32 = &ptr; | ||
let _ = *ptr_ptr as *const i32; | ||
} | ||
|
||
// Changes in mutability. Do not lint this. | ||
let _ = ptr as *mut i32; | ||
let _ = mut_ptr as *const i32; | ||
|
||
// `pointer::cast` cannot perform unsized coercions unlike `as`. Do not lint this. | ||
let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4]; | ||
let _ = ptr_of_array as *const [u32]; | ||
let _ = ptr_of_array as *const dyn std::fmt::Debug; | ||
|
||
// Ensure the lint doesn't produce unnecessary turbofish for inferred types. | ||
let _: *const i32 = ptr as *const _; | ||
let _: *mut i32 = mut_ptr as _; | ||
} | ||
|
||
fn _msrv_1_37() { | ||
#![clippy::msrv = "1.37"] | ||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
// `pointer::cast` was stabilized in 1.38. Do not lint this | ||
let _ = ptr as *const i32; | ||
let _ = mut_ptr as *mut i32; | ||
} | ||
|
||
fn _msrv_1_38() { | ||
#![clippy::msrv = "1.38"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I didn't know that inner attributes can also be put into functions. I always assumed that they only work on crate and module level. But it makes sense, that they work for every item(-block)-like thing. |
||
let ptr: *const u32 = &42_u32; | ||
let mut_ptr: *mut u32 = &mut 42_u32; | ||
|
||
let _ = ptr as *const i32; | ||
let _ = mut_ptr as *mut i32; | ||
} |
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,46 @@ | ||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:10:13 | ||
| | ||
LL | let _ = ptr as *const i32; | ||
| ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::<i32>()` | ||
| | ||
= note: `-D clippy::ptr-as-ptr` implied by `-D warnings` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:11:13 | ||
| | ||
LL | let _ = mut_ptr as *mut i32; | ||
| ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::<i32>()` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:16:17 | ||
| | ||
LL | let _ = *ptr_ptr as *const i32; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `(*ptr_ptr).cast::<i32>()` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:29:25 | ||
| | ||
LL | let _: *const i32 = ptr as *const _; | ||
| ^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast()` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:30:23 | ||
| | ||
LL | let _: *mut i32 = mut_ptr as _; | ||
| ^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast()` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:48:13 | ||
| | ||
LL | let _ = ptr as *const i32; | ||
| ^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `ptr.cast::<i32>()` | ||
|
||
error: `as` casting between raw pointers without changing its mutability | ||
--> $DIR/ptr_as_ptr.rs:49:13 | ||
| | ||
LL | let _ = mut_ptr as *mut i32; | ||
| ^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast`, a safer alternative: `mut_ptr.cast::<i32>()` | ||
|
||
error: aborting due to 7 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.
Uh oh!
There was an error while loading. Please reload this page.