Skip to content
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

[ER] Useless str -> String allocation #8088

Closed
Tracked by #79
leonardo-m opened this issue Dec 7, 2021 · 1 comment · Fixed by #12324
Closed
Tracked by #79

[ER] Useless str -> String allocation #8088

leonardo-m opened this issue Dec 7, 2021 · 1 comment · Fixed by #12324
Labels
A-lint Area: New lints

Comments

@leonardo-m
Copy link

What it does

Inspired also by:
rust-lang/rust#91626

In this code:

use std::collections::HashSet;
fn main() {
    let mut s = HashSet::from(["a".to_string(), "b".to_string()]);
    println!("{}", s.remove(&"b".to_string()));
}

I'd like Clippy to suggest to avoid the string allocation:

s.remove("b")

Similar suggestions are useful to HashMap, etc.

Lint Name

useless_string_allocation

Category

pedantic

Advantage

No response

Drawbacks

No response

Example

<code>

Could be written as:

<code>
@leonardo-m leonardo-m added the A-lint Area: New lints label Dec 7, 2021
@bugadani
Copy link
Contributor

bugadani commented Dec 7, 2021

Maybe this could be generalized to useless_allocation as well, for the Vec<->slice or similar cases?

bors added a commit that referenced this issue Feb 21, 2024
Extend `unnecessary_to_owned` to handle `Borrow` trait in map types

Fixes #8088.

Alternative to #12315.

r? `@y21`

changelog: Extend `unnecessary_to_owned` to handle `Borrow` trait in map types
@bors bors closed this as completed in 250fd09 Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
2 participants