Perhaps warn on a backwards Range
Nobody has claimed this yet.
- 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:
- Two indices
(a..b)wherebanda's values are known to be constant at compile time, andb < a. - A terminal index,
minin(a..min)whereminis the minimum value for the type, e.g. ifaisi32then(a..std::i32::MIN)would tickle it. Ifais 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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