-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Improve invalid_reference_casting
lint
#114784
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 2 commits into
rust-lang:master
from
Urgau:many-improve-invalid_reference_casting-lint
Aug 16, 2023
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
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 |
---|---|---|
@@ -1,104 +1,160 @@ | ||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:15:16 | ||
--> $DIR/reference_casting.rs:19:16 | ||
| | ||
LL | let _num = &mut *(num as *const i32 as *mut i32); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[deny(invalid_reference_casting)]` on by default | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:17:16 | ||
--> $DIR/reference_casting.rs:21:16 | ||
| | ||
LL | let _num = &mut *(num as *const i32).cast_mut(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:19:16 | ||
--> $DIR/reference_casting.rs:23:16 | ||
| | ||
LL | let _num = &mut *std::ptr::from_ref(num).cast_mut(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:21:16 | ||
--> $DIR/reference_casting.rs:25:16 | ||
| | ||
LL | let _num = &mut *std::ptr::from_ref({ num }).cast_mut(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:23:16 | ||
--> $DIR/reference_casting.rs:27:16 | ||
| | ||
LL | let _num = &mut *{ std::ptr::from_ref(num) }.cast_mut(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:25:16 | ||
--> $DIR/reference_casting.rs:29:16 | ||
| | ||
LL | let _num = &mut *(std::ptr::from_ref({ num }) as *mut i32); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:27:16 | ||
--> $DIR/reference_casting.rs:31:16 | ||
| | ||
LL | let _num = &mut *(num as *const i32).cast::<i32>().cast_mut(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:33:16 | ||
| | ||
LL | let _num = &mut *(num as *const i32).cast::<i32>().cast_mut().cast_const().cast_mut(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:35:16 | ||
| | ||
LL | let _num = &mut *(std::ptr::from_ref(static_u8()) as *mut i32); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:37:16 | ||
| | ||
LL | let _num = &mut *std::mem::transmute::<_, *mut i32>(num); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:31:16 | ||
--> $DIR/reference_casting.rs:41:16 | ||
| | ||
LL | let deferred = num as *const i32 as *mut i32; | ||
| ----------------------------- casting happend here | ||
LL | let _num = &mut *deferred; | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:44:16 | ||
| | ||
LL | let deferred = (std::ptr::from_ref(num) as *const i32 as *const i32).cast_mut() as *mut i32; | ||
| ---------------------------------------------------------------------------- casting happend here | ||
LL | let _num = &mut *deferred; | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:46:16 | ||
| | ||
LL | let _num = &mut *(num as *const _ as usize as *mut i32); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:50:9 | ||
| | ||
LL | &mut *((this as *const _) as *mut _) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:40:5 | ||
--> $DIR/reference_casting.rs:60:5 | ||
| | ||
LL | *(a as *const _ as *mut _) = String::from("Replaced"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:42:5 | ||
--> $DIR/reference_casting.rs:62:5 | ||
| | ||
LL | *(a as *const _ as *mut String) += " world"; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:44:5 | ||
--> $DIR/reference_casting.rs:64:5 | ||
| | ||
LL | *std::ptr::from_ref(num).cast_mut() += 1; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:46:5 | ||
--> $DIR/reference_casting.rs:66:5 | ||
| | ||
LL | *std::ptr::from_ref({ num }).cast_mut() += 1; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:48:5 | ||
--> $DIR/reference_casting.rs:68:5 | ||
| | ||
LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:50:5 | ||
--> $DIR/reference_casting.rs:70:5 | ||
| | ||
LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:52:5 | ||
--> $DIR/reference_casting.rs:72:5 | ||
| | ||
LL | *std::mem::transmute::<_, *mut i32>(num) += 1; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:56:5 | ||
--> $DIR/reference_casting.rs:76:5 | ||
| | ||
LL | let value = num as *const i32 as *mut i32; | ||
| ----------------------------- casting happend here | ||
LL | *value = 1; | ||
| ^^^^^^^^^^ | ||
|
||
error: aborting due to 16 previous errors | ||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:78:5 | ||
| | ||
LL | *(num as *const i32).cast::<i32>().cast_mut() = 2; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:80:5 | ||
| | ||
LL | *(num as *const _ as usize as *mut i32) = 2; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell` | ||
--> $DIR/reference_casting.rs:84:9 | ||
| | ||
LL | *(this as *const _ as *mut _) = a; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 25 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 there is high confusion potential between
ptr_const_cast
andptr_cast_const
. Maybe this one could be renamed toptr_mut_cast_const
?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 decided to rename the other one from
ptr_const_cast
toconst_ptr_cast
, so that every diagnostic item finishes with the name of it's method.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 that rename improved the situation but the two names are still only distinguished by the order of the components. I think the
mut
should still be added to this one.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.
Except that the
const
part is now on the opposite side of the word: in front forconst_ptr_cast
and behind forptr_cast_const
, there is still the possibility that someone might mistake one for the other, but I think it's sufficiently diffused.PS: I'm not against changing it, but I think adding
mut
would make the name out of place, and could definitely confuse a future me.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.
Mhh yeah the other names are also not prefixed with
mut_
, even though IMO they should be. I will approve this.