rust-lang / rust-lang/rust-clippy

Forbids use of `core::ops::RangeInclusive` in constants and statics.

Open
#11,928 0 comments 0 reactions 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

Forbids using core::ops::RangeInclusive in consts and (non-mutable) statics.

It would be nice if this could detect usage in struct fields (at for structs defined in the same crate), i.e.

Advantage

It's not too rare to work with static lists of inclusive ranges of... stuff. For example, this is especially common in unicode data, where they're often ranges of either char, or sometimes numeric types.

core::ops::RangeInclusive may seem natural for this, however it stores an extra field of exhausted: bool for the Iterator implementation which causes each range to be larger by at least one byte (almost always more than one, due to padding), increasing binary size and memory usage for no benefit (the relevant Iterator methods cannot be used on consts/statics).

Drawbacks

Some people don't care about such things, and it's mostly a problem when you have many of them.

(I don't care to argue that it should be on by default, a restriction/pedantic lint would be fine by me, even if it is pretty much always bad)

Example
const RANGES: &[RangeInclusive<char>] = &[
    '\u{0}'..='\u{7f}',
    '\u{80}'..='\u{7ff}',
    '\u{800}'..='\u{ffff}',
    '\u{10000}'..=char::MAX,
];

Could be written as:

const RANGES: &[(char, char)] = &[
    ('\u{0}', '\u{7f}'),
    ('\u{80}', '\u{7ff}'),
    ('\u{800}', '\u{ffff}'),
    ('\u{10000}', char::MAX),
];

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, tests, or entry points are named. Start by locating Clippy's existing lint implementations for const and static analysis, then define how the requested restriction and same-crate struct-field detection should be covered. Done means the intended RangeInclusive cases are diagnosed with tests while allowed cases remain unaffected.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.