Commit cd61be7
authored
changelog: [`sliced_string_as_bytes`]: don't fire on
`str[..].as_bytes()`
So I ran into this in some codebase I was working on,
where the lint fired on this line:
```rust
let string: &str;
string[..].as_bytes()
```
So I was trying to understand the rationale behind this lint, and it
says:
> It involves doing an unnecessary UTF-8 alignment check which is less
efficient, and can cause a panic.
This is obviously not true in the case where a `RangeFull` slice is
being taken, since there is no UTF-8 boundary check, and no panic can be
caused. So I created an exemption for `RangeFull`s.
Two other notes:
1. I'm not sure the word "alignment" in the lint's description (quoted
above) is really correct, should probably say "char boundary" instead?
2. I might be missing something, but isn't there a lint for doing
superfluous slice indexing, and then calling a slice method? e.g.
`str[..].len()` or `slice[..].len()`, where `str` and `slice` are `&str`
and `&[T]`, respectively. If we had one, I would expect *it* to fire for
the example code I quoted above.
File tree
3 files changed
+20
-2
lines changed- clippy_lints/src/methods
- tests/ui
3 files changed
+20
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
5 | | - | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
10 | 16 | | |
11 | 17 | | |
12 | | - | |
| 18 | + | |
13 | 19 | | |
14 | 20 | | |
15 | 21 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
35 | 41 | | |
36 | 42 | | |
37 | 43 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
35 | 41 | | |
36 | 42 | | |
37 | 43 | | |
0 commit comments