Lint to prevent people from indexing a string via iterator.nth(i) #6391
Labels
A-lint
Area: New lints
C-enhancement
Category: Enhancement of lints, like adding more cases or adding help messages
good-first-issue
These issues are a good way to get started with Clippy
L-suggestion
Lint: Improving, adding or fixing lint suggestions
What it does
It is possible to access the nth byte of a string using
string.bytes().nth(i)
. But in case ofi = 0
this collides with the lint iter-nth-zero.Alternatively one could write
string.as_bytes().get(1)
, which does not produce any clippy warnings. This code is equivalent to the code above in what it does.Categories (optional)
clippy::style
and maybeclippy::pedantic
. Maybe alsoclippy::perf
.Advantages
Drawbacks
The iteration is very easy for the compiler to optimise out, so that argument is weak.
The argument about iter-nth-zero is valid but it might be better to patch iter-nth-zero instead of teaching people how to code around the drawbacks of iter-nth-zero.
Maybe iter-nth-zero should become pedantic instead?
Example
Could be written as:
The text was updated successfully, but these errors were encountered: