rust-lang / rust-lang/rust-clippy

Lint against impl in block expressions when could be outside of block expression

Open
#2,124 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-style L-unnecessary T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Implementations exist globally no matter where they are defined. It's possible to define items in block expressions, and is done primarily for scoping where the item is accessible.

Since putting the implementation into a block expression does not actually limit scope, it should not be done unless absolutely required. Implementations in block expressions are only absolutely required when one of the item being implemented, the trait being implemented, or a generic bound (including in where clauses) refers to an item defined in the block expression.

One can also argue that if one has to define implementations in the block expression that you're being needlessly complex since you can always move both the implementation and the item it requires out of the block expression and into the containing module (or a submodule), and thus just disallow all implementations in block expressions.

The lint forbidding it because of complexity is easier to implement than the lint forbidding it only in confusing cases.

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

No source files or tests are named. Start by locating Clippy's existing lint registration and test patterns for implementation or block-expression lints, then clarify whether the intended rule covers all implementations in blocks or only cases where moving them is possible. Done means the selected cases are diagnosed consistently with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.