Skip to content

Warn about converting arguments to &str in std::process::Command's methods #5315

Open
@tesuji

Description

@tesuji

Categories: Performance, Correctness, warn-by-default

warn-by-default because the author may want to verify the UTF-8 validly
of the expression.

Auto-apply suggestion ability

None. It's hard to make correct suggestion.

What it does

Check for convert from lossless OsStr-compatible types (Like Path, PathBuf)
to &str when passing to arguments of std::process::Command methods:

  • Command::new
  • Command::arg
  • Command::args
  • Command::env
  • Command::envs
  • Command::env_remove
  • Command::current_dir

Known problems

The author may intend to verify the UTF-8 validly of the expression.

Example

use std::path::Path;
use std::process::Command;
// Assume this is the non UTF-8 path
let dest = Path::new("<non-utf8 path>");
Command::new("git")
    .args(&[
        "clone",
        "https://github.com/rust-lang/rust-clippy",
        &dest.to_string_lossy(),
    ])
    .status()
    .unwrap();

Could be written as:

use std::ffi::OsStr;
Command::new("git")
    .args(&[
        OsStr::new("clone"),
        OsStr::new("https://github.com/rust-lang/rust-clippy"),
        OsStr::new(dest),
    ])
    .status()
    .unwrap();

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