rust-lang / rust-lang/rust-clippy

New Lint: ambiguous closures like `return || true`

Open
#4,374 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The proposal is to implement a new lint to capture closures that are appear ambiguous to the reader. Such closures have no args, which makes the leading || look like a logical OR, and have an expression body without surrounding block. It may also only be ambiguous if the closure's body expression returns a boolean.

the classic example is return || true which at first glance looks like nonsense but actually returns a closure that always returns true.

Clippy should warn that this closure is potentially ambiguous to the reader and may be misinterpreted as boolean logic. It would also be nice if it suggests a machine applicable solution so that rustfix can benefit from the lint.

Possible suggestions include

return || { true }

return { || true }

return { || { true } }

Though I think the former is still ambiguous

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

The issue names no implementation files, tests, or entry points. Start by locating the existing Clippy lint and UI-test conventions for closure syntax, then resolve which zero-argument expression closures should be diagnosed and which machine-applicable suggestion is acceptable. Done means the new lint and its selected fix are covered by tests.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.