-
Notifications
You must be signed in to change notification settings - Fork 1.6k
unnecessary_string_new
#8650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for opening this issue! Looking at the godbolt, even with This lint might be similar to the But the |
/u/lebensterben also suggested the following on the Reddit post (which could be a separate lint):
|
@flip1995 apparently the reason this performs the same is that there isn't an actual allocation,
I'm assuming this is still valid for stylistic reasons, also needs to be checked if 100% of the additional output from |
/u/1vader added that |
`unnecessary_owned_empty_string` [`unnecessary_owned_empty_string`] Fixes #8650 - \[x] Followed [lint naming conventions][lint_naming] - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - \[x] Added lint documentation - \[x] Run `cargo dev fmt` [lint_naming]: https://rust-lang.github.io/rfcs/0344-conventions-galore.html#lints changelog: Adds `unnecessary_owned_empty_string`, a lint that detects passing owned empty strings to a function expecting `&str`
What it does
Detects cases of
&String::new()
being passed to something expecting&str
.This is a followup on @flip1995's suggestion:
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
Could be written as:
The text was updated successfully, but these errors were encountered: