rust-lang / rust-lang/rust-clippy

Prevent "soft" memory leaks by suggesting to shrink

Open
#10,079 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Vecs, Strings, and other heap-allocated types, don't automatically shrink their capacity when the len() is small enough. This lint acts as a reminder to shrink whenever there's a call to remove

Lint Name

remove_without_shrink

Category

perf, restriction

Advantage
  • reminds to free unused memory
  • good for beginners and experienced devs alike, who are unaware of the lack of auto-shrink, or have too much cognitive load (respectively)
Drawbacks
  • too many false positives, unless it's smarter (i.e. only triggering if multiple elements are removed at once)
Example
vector.remove(0);

Could be written as:

vector.remove(0);
vector.shrink_to_fit();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing existing Clippy lints for Vec and String removal and capacity handling. Define when remove_without_shrink should trigger, including how to avoid the false positives noted for single removals. Done means the lint behavior, applicability, and tests cover the agreed cases.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.