Skip to content

Using Path::display to construct a new Path #17157

@estebank

Description

@estebank

What it does

Detect when FS operations are being done using a fresly-constructed String from Paths. Instead, devs should be using Path::join to avoid an unnecessarily lossy transformation.

Advantage

Code is more robust to paths that are not representable by UTF-8 strings.

Drawbacks

The detection logic might be too difficult to accomplish for the general case.

The correct code might be difficult to automatically suggest.

Example

    let p = "foo";
    let bar = "bar";
    let _ = fs::create_file(format!("{}/bar", p.display()));
    let _ = fs::create_file(format!("{}/{}", p.dispaly(), bar.display()));

Could be written as:

    let _ = fs::create_file(p.join("bar"));
    let _ = fs::create_file(p..join(bar));

Comparison with existing lints

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions