rust-lang / rust-lang/rfcs

Perhaps warn on a backwards Range

Open
#1,297 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-compiler T-dev-tools
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Recently I did the following:

for i in (idx..0) { 
    //stuff
}

There the type of idx was usize.

This, of course, does not exactly do what is intended (count backwards from idx to 0), and I couldn't find the bug for a while. The iterator does absolutely nothing at all, it doesn't even panic. I think it may be useful to have a warning emitted when this can be detected. I think the following two cases should be surefire:

  1. Two indices (a..b) where b and a's values are known to be constant at compile time, and b < a.
  2. A terminal index, min in (a..min) where min is the minimum value for the type, e.g. if a is i32 then (a..std::i32::MIN) would tickle it. If a is a usize then (a..0) would tickle it. This is because a range-exclusive Range like .. can only produce an empty iterator if the last index is min, so it's almost certainly an attempt at a backwards iterator.

There may be other provable cases, but these are the easy ones.

Contributor guide

No contributing guide indexed for this repository

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 files, tests, or implementation entry points; begin by reviewing its two proposed compile-time cases and the RFC process. Done means deciding whether a backwards-Range warning belongs in Rust and documenting the accepted scope and validation needed.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.