Skip to content

unnecessary_string_new #8650

Closed
Closed
@yoav-lavi

Description

@yoav-lavi

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("");

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-styleLint: Belongs in the style lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions