rust-lang / rust-lang/rfcs

Strengthen linear typing via #[must_use(dispose)]

Open
#2,642 6 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-attributes A-lint A-typesystem T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

#[must_use] has a hole in its coverage. Suppose we have some code like this:

let transaction = Transaction::new(&kitchen)
    .catch_frogs(3)
    .set_gas_flow(2.5);
if transaction.is_boiling() {
    println!("How I love a nice bowl of frog soup!");
}

At this point, #[must_use] is satisfied merely because transaction has been inspected (via &self), but the call to transaction.commit() has been forgotten and there may be an error to rollback. What we want is for there to be a warning if the type has not been properly disposed of.

A type can be disposed of by destructuring, or by passing it off to another function.

Some consequences:

  1. mem::drop counts as disposal. It won't be infallible for the same reasons that Drop isn't infallible.
  2. Traits like trait Foo { fn foo(self) {} } would expose a hole. And it could be a blanket implementation.
  3. A method can ignore self by using let Self { .. } = self.

Result could be marked #[must_use(dispose)]. But I don't think it should be.

Alternatives

  • Do nothing, and change fn inspect(&self) -> bool to fn inspect(self) -> (Self, bool).
  • Do nothing.
  • Implement with clippy instead.
  • A different syntax, like #[must_dispose].
  • Make #[must_use] function like the proposed #[must_use(dispose)].

Contributor guide

No contributing guide indexed for this repository

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

No files, tests, or implementation entry points are named. Begin by resolving the proposed #[must_use(dispose)] semantics and comparing the listed alternatives; the issue is ready only when a concrete direction and acceptance criteria are documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.