rust-lang / rust-lang/rust-clippy

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

Open
#5,315 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-style
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining the listed std::process::Command methods and the provided Rust example to determine how lossless OsStr-compatible values are converted to &str. Done means the lint detects these conversions, emits a warning without an auto-apply suggestion, and accounts for the stated UTF-8 validation concern.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.