Skip to content

Inefficient usage of PathBuf #5072

@sinkuu

Description

@sinkuu

PathBuf comes with inherent mutating methods like push, set_extension, etc. along with methods come from Deref<Target = Path> like join, with_extension, etc.

Each usage of the latter group of methods throws current PathBuf away and creates a new one:

let mut p: PathBuf;

// bad (in terms of performance)
p
 .join("x")              // &PathBuf -> PathBuf
 .with_extension("txt"); // &PathBuf -> PathBuf

// good
p.push("x");
p.set_extension("txt");

The lint to detect this situation may be under perf, but we might as well make it pedantic since the former code looks more clean and idiomatic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsL-perfLint: Belongs in the perf lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions