Skip to content

[ER] Useless str -> String allocation #8088

Closed
@leonardo-m

Description

@leonardo-m

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions