rust-lang / rust-lang/rust-clippy
box_collection lint shouldn't trigger when used in `extern "C"` functions
Open
Nobody has claimed this yet.
C-bug
I-false-positive
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
Clippy warns about Box<Vec<T>> as being a useless heap allocation, but when they are used in extern "C" functions the Box is necessary as Vec's layout isn't repr(C).
Lint Name
box_collection
Reproducer
I tried this code:
#[no_mangle]
pub extern "C" fn vec_tag_new() -> Box<Vec<u32>> {
Box::new(Vec::new())
}
I saw this happen:
error: you seem to be trying to use `Box<Vec<..>>`. Consider using just `Vec<..>`
--> src/lib.rs:283:36
|
283 | pub extern "C" fn vec_tag_new() -> Box<Vec<u32>> {
| ^^^^^^^^^^^^^
|
= note: `-D clippy::box-collection` implied by `-D warnings`
= help: `Vec<..>` is already on the heap, `Box<Vec<..>>` makes an extra allocation
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#box_collection
I expected to see this happen:
No lint.
Version
rustc 1.59.0 (9d1b2106e 2022-02-23)
binary: rustc
commit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a
commit-date: 2022-02-23
host: x86_64-apple-darwin
release: 1.59.0
LLVM version: 13.0.0
Additional Labels
No response
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 with the box_collection lint implementation and its existing tests, then inspect how the lint handles function signatures and extern "C" items. Reproduce the shown Box<Vec> case and add coverage showing that it is not linted in an extern "C" function while the usual case still is.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100