rust-lang / rust-lang/rust

Lint overcapturing in impl Trait on 2024 edition

Open
#131,943 1 comment 0 reactions 1 assignee View on GitHub

@compiler-errors is already working on this.

Since Oct 19, 2024.

A-diagnostics A-lints C-enhancement T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
pub fn check(x: &[u8]) -> impl std::fmt::Display {
    x[0]
}
Current output

(empty)

Desired output

A warning, like one produced by impl_trait_overcaptures.

Rationale and extra context

Currently, impl_trait_overcaptures lints code that would be overcapturing in edition = 2024 on editions < 2024. I think it would be valuable to add a (maybe allow-by-default) lint to detect overcapturing in new, edition 2024 code. Code like this is probably not meant to capture lifetime of x, so linting against this may help to avoid too-strict signatures. If the function was meant to be stricter than necessary (e.g. for future-compat reasons), you can always just #[allow] the lint.

Rust Version
$ rustc --version --verbose
rustc 1.83.0-nightly (eb4e23467 2024-10-09)
binary: rustc
commit-hash: eb4e2346748e1760f74fcaa27b42431e0b95f8f3
commit-date: 2024-10-09
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.1
Anything else?

Alternatively, this could be a Clippy lint. Having it in rustc could probably allow code reuse with the edition lint though.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.