Open woodruffw opened 1 year ago
Note for anyone implementing this. rsplit
doesn't have the same result as reversing the order of split
, only some cases do. e.g. "x:::y".split("::")
gives ["x", ":y"]
, but rsplit("::")
gives ["y", "x:"]
.
Thanks for pointing that out, I didn't realize there were differences between the two. It sounds like a high-quality lint here will have to be opt in because of unreliability, then.
Edit: I misread the example as x::y
, not x:::y
. The difference for the latter makes sense.
@alex pointed out that there is actually a sound solution here: any needle of size 1 or that isn't a palindrome should be equivalent between the two for all inputs. Anything else may be equivalent for some inputs but not others.
A palindrome isn't the right condition, though they will always cause a problem. If a proper prefix of the pattern is also a suffix then the two will behave differently. e.g. splitting ab.ab
on 1ab.ab.ab1
will work differently with split
and rsplit
.
What it does
A user working with a string might forget that
str::rsplit
exists, and design an API like this:which could be rewritten as:
Clippy does not currently detect this.
Advantage
Vec<_>
or other owned container type)Drawbacks
None that I can think of.
Example
Could be written as: