rust-lang / rust-lang/rust-clippy
Deny when pointer to temporary stored somewhere
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
It should deny this code:
struct MyStruct{
field: usize
}
let my_struct_ptr: *mut MyStruct = ...;
let reference_to_field = &mut unsafe { (*my_struct_ptr).field };
// use reference here
This code actually creates temporary with value of field, takes reference to it, then drops temporary.
Therefore here we have dangling pointer to stack.
It should suggest this:
let reference_to_field = unsafe { &mut (*my_struct_ptr).field };
Categories (optional)
- Kind: clippy::correctness
- Remove UB code created in unsafe block
Drawbacks
None.
Example
let reference_to_field = &mut unsafe { (*my_struct_ptr).field };
Should be written as:
let reference_to_field = unsafe { &mut (*my_struct_ptr).field };
Also
Link to issue which caused by original code: https://github.com/Morganamilo/paru/issues/392
Bugfix: https://github.com/archlinux/alpm.rs/commit/5253d6
Relevant Reddit discussion.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the two Rust examples and search rust-clippy's correctness lints for the relevant implementation entry point. The work is done when the first form is denied as a dangling reference risk and the second form is accepted, with the suggested rewrite matching the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100