rust-lang / rust-lang/rust-clippy
Move `unsafe` inside closures
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
Rust currently allows the following code:
let foo = unsafe { || bar(someArg) + 1 };
foo();
I believe that this suboptimal, but foo it feels like calling foo should require another unsafe block. Of course, since the Fn, FnMut, and FnOnce traits do not allow unsafe methods, foo can be called with unsafe.
To better reflect this behavior, I believe the above code should be written as such instead:
let foo = || unsafe { bar(someArg) + 1 };
foo();
This more clearly communicates that a caller of foo does not need unsafe.
This also extends to closures with arguments. Since the closure cannot be unsafe, any input arguments must not result in UB. A similar argument can be made for captures, since they can be thought of as implicit arguments.
Advantage
- The code more closely represents the actual call behavior of the closure.
Drawbacks
- None that I can think of right now.
Example
let foo = unsafe { || bar(someArg) + 1 };
Could be written as:
let foo = || unsafe { bar(someArg) + 1 };
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
The issue names no files, tests, or entry points. Start by understanding Rust's closure and unsafe semantics in the two examples, then locate the Clippy lint area that handles closure safety; done means the proposed placement of unsafe is consistently diagnosed and covered by tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100