Skip to content

Add lint on &[char] #5598

Closed
Closed
@SoniEx2

Description

@SoniEx2

Since first writing this issue, all the following have come to our attention. Originally only the first point was written here:

  1. &[char] doesn't provide O(1) indexing on characters, but on codepoints. a single character can be multiple codepoints (see also char.to_uppercase, which can output one or more chars!), in this case &[&str] (or equivalent) is more appropriate.
  2. &[char] also doesn't provide O(1) indexing on columns, for some definition of "column". a single column can be multiple codepoints.
  3. There is confusion about what &[char] actually means in a Pattern. As an example "___abc___".split(&['a', 'b', 'c'][..]).collect::<Vec<_>>() makes ["___", "", "", "___"] but some may expect it to make ["___", "___"].
    1. One way someone may expect this is as Pattern for &[char] being UTF-32. It is not. It is well-documented that Rust doesn't support UTF-32 in std.
    2. Another way is "any combination of these chars". It is also not, as that would break str.strip_prefix.
  4. Even if there wasn't such confusion, &[char] may be slower than a hypothetical naive &[&str] for a small enough n, as the former requires encoding/decoding UTF-8 whereas the latter can rely on UTF-8 being a self-synchronizing prefix-free code and just match bytes.
  5. If you actually wanted to use an &[char] as a Pattern, but you wanted to uppercase the chars for some reason... well, see (1).
  6. For large n, &[char]: Pattern is slow. You should under no circumstances use ('a'..='z').collect::<Vec<_>>().as_slice() as a Pattern. Use |c| matches!(c, 'a'..='z') instead.

As such, there are a whole lot of issues with &[char] in practice. We think it's a good idea to lint against it.

Metadata

Metadata

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions