Description
What it does
Detects cases of &String::new()
being passed to something expecting &str
.
This is a followup on @flip1995's suggestion:
A separate lint about using an owned empty string where also a "" would be possible might be better to put time in than trying to cover all cases in this lint.
When checking godbolt.org it seems that &String::new()
does an extra allocation although in benchmarks they seemed to perform similarly (which leads me to believe it may be optimized away in some cases, or that the allocation is almost insignificant in this case).
See examples of this happening here (might include cases of &String
)
I also opened a question on /r/rust to make sure there aren't cases that this would be valid.
Lint Name
unnecessary_string_new
Category
style
Advantage
Shorter, possibly more performant
Drawbacks
None that I'm aware of
Example
"12345".split(&String::new());
Could be written as:
"12345".split("");