rust-lang/rust-clippy

Lint for .clone().into_boxed_str()

开放

#15,951 创建于 2025年10月25日

 (4 条评论) (0 个反应) (1 位负责人)Rust (1,391 个派生)batch import
A-lintgood first issue

仓库指标

星标
 (10,406 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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.

贡献者指南