Skip to content

Lint for .clone().into_boxed_str() #15951

@lolbinarycat

Description

@lolbinarycat

What it does

Suggest turning s.clone().into_boxed_str() into Box::from(s.as_str()).

Possibly could be grouped together with other inefficient string conversions.

Advantage

Removes a call to __rust_realloc

Drawbacks

No response

Example

fn foo(s: &String) -> Box<str> {
    s.clone().into_boxed_str()
}

Could be written as:

fn foo(s: &String) -> Box<str> {
    Box::from(s.as_str())
}

Comparison with existing lints

redundant_clone generally doesn't consider the semantics of the

Additional Context

A GitHub search shows this unoptimal pattern is actually used fairly often.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions