Open LikeLakers2 opened 1 week ago
As an aside to this: I don't know if we can apply this same logic to something like let my_len = my_string[start..end].len()
(by suggesting it be changed to let my_len = end - start
).
The math does check out the same - but with the suggested version, the panic that occurs if start > end
would only occur with overflow checks enabled; whereas with the string slicing operation, that condition is always checked.
This could be solved by having clippy add an assert, but I don't know if it'd be acceptable to have clippy doing that.
In my opinion the lint message should include something like 'if you want to count the amount of characters in this slice use .chars().count()', as that might be intended by the programmer. (inspired by warnings in docs of str::len
and str::chars
)
'if you want to count the amount of characters in this slice use .chars().count()',
⚠️ str::chars().count()
does not return the number of characters in a string.
(Some of these may look weird in the code editor, but will display properly in the exection output.)
What it does
I apologize if this comes across as confusing - I'm not entirely sure how to word it.
This lint would look for instances where the user is getting a slice of a string, with the starting point of the range being
0
, and then callinglen()
on that slice.The reason I believe this works is because
impl SliceIndex<str> for Range<usize>
states the following:This means that, if you're getting a string slice from
0
tosome_var
,some_var
will already need to be a length in bytes. Thus,some_string[0..some_calculated_len]
will always besome_calculated_len
bytes long. And thus,some_string[0..some_calculated_len].len()
will always be the same as justsome_calculated_len
.Advantage
Less complex-looking code, while still functioning the same.
Drawbacks
None that I'm aware of - the code should function exactly the same afterwards.
Example
Could be written as: