rust-lang / rust-lang/rust-clippy

"Forwards compatibility"-breaking lints for const features

Open
#16,722 0 comments 1 reaction 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

Today I would like to write const fns that use language features such as loops. Due to the limitations in const-context today, these functions have to be written in an unidiomatic way.

Tomorrow, when features like iterators and for loops are available in const-context, I would like clippy to remind me to revisit these functions.

A complex formulation would be a lint that pattern matches the body of const fns looking for evidence that things could be rewritten. (eg, #15046)

A simpler formulation would be a lint that:

  • a) is opt-in only
  • b) today, never fires
  • c) tomorrow, when (eg) iterators/for loops are const, fires blindly without looking at the function body

This means, today I would write:

#[warn(clippy::revisit_const_for, clippy::revisit_const_iter)]
pub const fn bloorp() {
  let mut i = 0;
  while i < 10 { i += 1; ... }
}

Tomorrow, when iterators/for loops are const in the project's MSRV, the lint would start firing and remind me. I would rewrite the function and remove the #[warn].

Advantage

Currently const code that uses loops is very odd-looking, and insidious because it works fine.

Drawbacks

It's a bit weird and unlike other lints(?). There is no possibility of suggestions. It means clippy becomes a TODO app. It probably needs a lint per unstable feature, and the code author to predict which features need stabilization to allow their code to become const.

Example
#[warn(clippy::revisit_const_for, clippy::revisit_const_iter)]
pub const fn bloorp() {
  let mut i = 0;
  while i < 10 { i += 1; ... }
}

When for loops are available in const context, the lint would fire. Now the code would be written as:

pub const fn bloorp() {
  for i in 0..10 { ... }
}
Comparison with existing lints

No response

Additional Context

No response

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 how Clippy lints track const-context language features and how stabilized features are represented. The issue names no files, tests, or entry points, so identify those before assessing the proposed opt-in revisit lints. Done means reaching a project decision on whether this design should be implemented and how its feature-specific behavior would be defined.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.