-
Notifications
You must be signed in to change notification settings - Fork 1.7k
cast_ref_to_mut lint #3600
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
cast_ref_to_mut lint #3600
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
34daf09
cast_ref_to_mut lint
LunaBorowska fd57874
Use ty::Ref instead of ty::TyKind::Ref
LunaBorowska 1cab4d1
Add a note to cast_ref_to_mut lint
LunaBorowska 6faf133
Move a hint to an error message in cast_ref_to_mut lint
LunaBorowska 21d3045
Don't import ty::Ref in cast_ref_to_mut lint
LunaBorowska 27ea638
Move cast_ref_to_mut list to correctness group
LunaBorowska 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#![warn(clippy::cast_ref_to_mut)] | ||
#![allow(clippy::no_effect)] | ||
|
||
extern "C" { | ||
// NB. Mutability can be easily incorrect in FFI calls, as | ||
// in C, the default are mutable pointers. | ||
fn ffi(c: *mut u8); | ||
fn int_ffi(c: *mut i32); | ||
} | ||
|
||
fn main() { | ||
let s = String::from("Hello"); | ||
let a = &s; | ||
unsafe { | ||
let num = &3i32; | ||
let mut_num = &mut 3i32; | ||
// Should be warned against | ||
(*(a as *const _ as *mut String)).push_str(" world"); | ||
*(a as *const _ as *mut _) = String::from("Replaced"); | ||
*(a as *const _ as *mut String) += " world"; | ||
// Shouldn't be warned against | ||
println!("{}", *(num as *const _ as *const i16)); | ||
println!("{}", *(mut_num as *mut _ as *mut i16)); | ||
ffi(a.as_ptr() as *mut _); | ||
int_ffi(num as *const _ as *mut _); | ||
int_ffi(&3 as *const _ as *mut _); | ||
let mut value = 3; | ||
let value: *const i32 = &mut value; | ||
*(value as *const i16 as *mut i16) = 42; | ||
} | ||
} |
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,22 @@ | ||
error: casting &T to &mut T may cause undefined behaviour, consider instead using an UnsafeCell | ||
--> $DIR/cast_ref_to_mut.rs:18:9 | ||
| | ||
LL | (*(a as *const _ as *mut String)).push_str(" world"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::cast-ref-to-mut` implied by `-D warnings` | ||
|
||
error: casting &T to &mut T may cause undefined behaviour, consider instead using an UnsafeCell | ||
--> $DIR/cast_ref_to_mut.rs:19:9 | ||
| | ||
LL | *(a as *const _ as *mut _) = String::from("Replaced"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: casting &T to &mut T may cause undefined behaviour, consider instead using an UnsafeCell | ||
--> $DIR/cast_ref_to_mut.rs:20:9 | ||
| | ||
LL | *(a as *const _ as *mut String) += " world"; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 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.