-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Clippy pointer cast lint experiment #75098
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
bors
merged 23 commits into
rust-lang:master
from
Ryan1729:clippy-pointer-cast-lint-experiment
Aug 11, 2020
Merged
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
ef29161
make parts of rustc_typeck public
Ryan1729 5565c1a
run cargo dev new_lint then move transmutes_expressible_as_ptr_casts …
Ryan1729 0c37239
make InheritedBuilder::enter public
Ryan1729 b4e6e70
initial compiling version of TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS
Ryan1729 7061b1c
write currently failing test for transmutes_expressible_as_ptr_casts
Ryan1729 b4ecee9
accidentally cause an ICE by putting the TRANSMUTES_EXPRESSIBLE_AS_PT…
Ryan1729 1e5c14d
try putting the can_be_expressed_as_pointer_cast at the top and find …
Ryan1729 7a818c0
get the expected number of errors by acknowledging that other lints a…
Ryan1729 ab93133
address some review comments
Ryan1729 6fdd1db
add description to assert
Ryan1729 8ba4101
add documentation to functions that call `do_check` and add a test ag…
Ryan1729 afd4909
add extra error message to the expected stderr for transmutes_express…
Ryan1729 5447247
change filter to assert, and update comments
Ryan1729 e3170bb
add newline to transmutes_expressible_as_ptr_casts.rs
Ryan1729 4027f15
run ./x.py fmt
Ryan1729 007dc94
run clippy_dev update_lints
Ryan1729 32691da
run clippy_dev fmt
Ryan1729 d897fd2
Apply suggestions from code review
Ryan1729 c04c4cb
copy over *.fixed file
Ryan1729 b02bf05
update stderr for transmutes_expressible_as_ptr_casts
Ryan1729 0fbf450
add a test example of where transmutes_expressible_as_ptr_casts shoul…
Ryan1729 58b8b11
fix unary minus on usize and unused variable errors in .fixed file
Ryan1729 d2e7293
add allow unused_unsafe and allow dead_code
Ryan1729 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
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
76 changes: 76 additions & 0 deletions
76
src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.rs
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,76 @@ | ||
#![warn(clippy::transmutes_expressible_as_ptr_casts)] | ||
Ryan1729 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// These two warnings currrently cover the cases transmutes_expressible_as_ptr_casts | ||
// would otherwise be responsible for | ||
#![warn(clippy::useless_transmute)] | ||
#![warn(clippy::transmute_ptr_to_ptr)] | ||
|
||
use std::mem::transmute; | ||
|
||
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is | ||
// valid, which we quote from below. | ||
|
||
fn main() { | ||
// We should see an error message for each transmute, and no error messages for | ||
// the casts, since the casts are the recommended fixes. | ||
|
||
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast | ||
let ptr_i32_transmute = unsafe { | ||
transmute::<isize, *const i32>(-1) | ||
}; | ||
let ptr_i32 = -1isize as *const i32; | ||
|
||
// e has type *T, U is *U_0, and either U_0: Sized ... | ||
let ptr_i8_transmute = unsafe { | ||
transmute::<*const i32, *const i8>(ptr_i32) | ||
}; | ||
let ptr_i8 = ptr_i32 as *const i8; | ||
|
||
let slice_ptr = &[0,1,2,3] as *const [i32]; | ||
|
||
// ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast | ||
let ptr_to_unsized_transmute = unsafe { | ||
transmute::<*const [i32], *const [u16]>(slice_ptr) | ||
}; | ||
let ptr_to_unsized = slice_ptr as *const [u16]; | ||
// TODO: We could try testing vtable casts here too, but maybe | ||
// we should wait until std::raw::TraitObject is stabilized? | ||
|
||
// e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast | ||
let usize_from_int_ptr_transmute = unsafe { | ||
transmute::<*const i32, usize>(ptr_i32) | ||
}; | ||
let usize_from_int_ptr = ptr_i32 as usize; | ||
|
||
let array_ref: &[i32; 4] = &[1,2,3,4]; | ||
|
||
// e has type &[T; n] and U is *const T; array-ptr-cast | ||
let array_ptr_transmute = unsafe { | ||
transmute::<&[i32; 4], *const [i32; 4]>(array_ref) | ||
}; | ||
let array_ptr = array_ref as *const [i32; 4]; | ||
|
||
fn foo(_: usize) -> u8 { 42 } | ||
|
||
// e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast | ||
let usize_ptr_transmute = unsafe { | ||
transmute::<fn(usize) -> u8, *const usize>(foo) | ||
}; | ||
let usize_ptr_transmute = foo as *const usize; | ||
|
||
// e is a function pointer type and U is an integer; fptr-addr-cast | ||
let usize_from_fn_ptr_transmute = unsafe { | ||
transmute::<fn(usize) -> u8, usize>(foo) | ||
}; | ||
let usize_from_fn_ptr = foo as *const usize; | ||
} | ||
|
||
// If a ref-to-ptr cast of this form where the pointer type points to a type other | ||
// than the referenced type, calling `CastCheck::do_check` has been observed to | ||
// cause an ICE error message. `do_check` is currently called inside the | ||
// `transmutes_expressible_as_ptr_casts` check, but other, more specific lints | ||
// currently prevent it from being called in these cases. This test is meant to | ||
// fail if the ordering of the checks ever changes enough to cause these cases to | ||
// fall through into `do_check`. | ||
fn trigger_do_check_to_emit_error(in_param: &[i32; 1]) -> *const u8 { | ||
unsafe { transmute::<&[i32; 1], *const u8>(in_param) } | ||
} |
56 changes: 56 additions & 0 deletions
56
src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.stderr
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,56 @@ | ||
error: transmute from an integer to a pointer | ||
--> $DIR/transmutes_expressible_as_ptr_casts.rs:18:9 | ||
| | ||
LL | transmute::<isize, *const i32>(-1) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `-1 as *const i32` | ||
| | ||
= note: `-D clippy::useless-transmute` implied by `-D warnings` | ||
|
||
error: transmute from a pointer to a pointer | ||
--> $DIR/transmutes_expressible_as_ptr_casts.rs:24:9 | ||
| | ||
LL | transmute::<*const i32, *const i8>(ptr_i32) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8` | ||
| | ||
= note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings` | ||
|
||
error: transmute from a pointer to a pointer | ||
--> $DIR/transmutes_expressible_as_ptr_casts.rs:32:9 | ||
| | ||
LL | transmute::<*const [i32], *const [u16]>(slice_ptr) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `slice_ptr as *const [u16]` | ||
|
||
error: transmute from `*const i32` to `usize` which could be expressed as a pointer cast instead | ||
--> $DIR/transmutes_expressible_as_ptr_casts.rs:40:9 | ||
| | ||
LL | transmute::<*const i32, usize>(ptr_i32) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize` | ||
| | ||
= note: `-D clippy::transmutes-expressible-as-ptr-casts` implied by `-D warnings` | ||
|
||
error: transmute from a reference to a pointer | ||
--> $DIR/transmutes_expressible_as_ptr_casts.rs:48:9 | ||
| | ||
LL | transmute::<&[i32; 4], *const [i32; 4]>(array_ref) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]` | ||
|
||
error: transmute from `fn(usize) -> u8 {main::foo}` to `*const usize` which could be expressed as a pointer cast instead | ||
--> $DIR/transmutes_expressible_as_ptr_casts.rs:56:9 | ||
| | ||
LL | transmute::<fn(usize) -> u8, *const usize>(foo) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize` | ||
|
||
error: transmute from `fn(usize) -> u8 {main::foo}` to `usize` which could be expressed as a pointer cast instead | ||
--> $DIR/transmutes_expressible_as_ptr_casts.rs:62:9 | ||
| | ||
LL | transmute::<fn(usize) -> u8, usize>(foo) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize` | ||
|
||
error: transmute from a reference to a pointer | ||
--> $DIR/transmutes_expressible_as_ptr_casts.rs:75:14 | ||
| | ||
LL | unsafe { transmute::<&[i32; 1], *const u8>(in_param) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `in_param as *const [i32; 1] as *const u8` | ||
|
||
error: aborting due to 8 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.