Skip to content

Commit 298f139

Browse files
committed
Auto merge of #10317 - m-ou-se:suspicious-command-arg-space, r=Manishearth
Add `suspicious_command_arg_space` lint Fixes #10316 --- changelog: New lint: [`suspicious_command_arg_space`] [#10317](#10317) <!-- changelog_checked -->
2 parents 6f353fd + 1f77866 commit 298f139

File tree

7 files changed

+108
-0
lines changed

7 files changed

+108
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,6 +4764,7 @@ Released 2018-09-13
47644764
[`suboptimal_flops`]: https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flops
47654765
[`suspicious_arithmetic_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_arithmetic_impl
47664766
[`suspicious_assignment_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_assignment_formatting
4767+
[`suspicious_command_arg_space`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_command_arg_space
47674768
[`suspicious_else_formatting`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_else_formatting
47684769
[`suspicious_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_map
47694770
[`suspicious_op_assign_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_op_assign_impl

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
378378
crate::methods::SKIP_WHILE_NEXT_INFO,
379379
crate::methods::STABLE_SORT_PRIMITIVE_INFO,
380380
crate::methods::STRING_EXTEND_CHARS_INFO,
381+
crate::methods::SUSPICIOUS_COMMAND_ARG_SPACE_INFO,
381382
crate::methods::SUSPICIOUS_MAP_INFO,
382383
crate::methods::SUSPICIOUS_SPLITN_INFO,
383384
crate::methods::SUSPICIOUS_TO_OWNED_INFO,

clippy_lints/src/methods/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ mod skip_while_next;
8080
mod stable_sort_primitive;
8181
mod str_splitn;
8282
mod string_extend_chars;
83+
mod suspicious_command_arg_space;
8384
mod suspicious_map;
8485
mod suspicious_splitn;
8586
mod suspicious_to_owned;
@@ -3162,6 +3163,32 @@ declare_clippy_lint! {
31623163
"collecting an iterator when collect is not needed"
31633164
}
31643165

3166+
declare_clippy_lint! {
3167+
/// ### What it does
3168+
///
3169+
/// Checks for `Command::arg()` invocations that look like they
3170+
/// should be multiple arguments instead, such as `arg("-t ext2")`.
3171+
///
3172+
/// ### Why is this bad?
3173+
///
3174+
/// `Command::arg()` does not split arguments by space. An argument like `arg("-t ext2")`
3175+
/// will be passed as a single argument to the command,
3176+
/// which is likely not what was intended.
3177+
///
3178+
/// ### Example
3179+
/// ```rust
3180+
/// std::process::Command::new("echo").arg("-n hello").spawn().unwrap();
3181+
/// ```
3182+
/// Use instead:
3183+
/// ```rust
3184+
/// std::process::Command::new("echo").args(["-n", "hello"]).spawn().unwrap();
3185+
/// ```
3186+
#[clippy::version = "1.67.0"]
3187+
pub SUSPICIOUS_COMMAND_ARG_SPACE,
3188+
suspicious,
3189+
"single command line argument that looks like it should be multiple arguments"
3190+
}
3191+
31653192
pub struct Methods {
31663193
avoid_breaking_exported_api: bool,
31673194
msrv: Msrv,
@@ -3289,6 +3316,7 @@ impl_lint_pass!(Methods => [
32893316
SEEK_FROM_CURRENT,
32903317
SEEK_TO_START_INSTEAD_OF_REWIND,
32913318
NEEDLESS_COLLECT,
3319+
SUSPICIOUS_COMMAND_ARG_SPACE,
32923320
]);
32933321

32943322
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3496,6 +3524,9 @@ impl Methods {
34963524
unnecessary_lazy_eval::check(cx, expr, recv, arg, "and");
34973525
}
34983526
},
3527+
("arg", [arg]) => {
3528+
suspicious_command_arg_space::check(cx, recv, arg, span);
3529+
}
34993530
("as_deref" | "as_deref_mut", []) => {
35003531
needless_option_as_deref::check(cx, expr, recv, name);
35013532
},
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::paths;
3+
use clippy_utils::ty::match_type;
4+
use rustc_ast as ast;
5+
use rustc_errors::{Applicability, Diagnostic};
6+
use rustc_hir as hir;
7+
use rustc_lint::LateContext;
8+
use rustc_span::Span;
9+
10+
use super::SUSPICIOUS_COMMAND_ARG_SPACE;
11+
12+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, recv: &'tcx hir::Expr<'_>, arg: &'tcx hir::Expr<'_>, span: Span) {
13+
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
14+
15+
if match_type(cx, ty, &paths::STD_PROCESS_COMMAND)
16+
&& let hir::ExprKind::Lit(lit) = &arg.kind
17+
&& let ast::LitKind::Str(s, _) = &lit.node
18+
&& let Some((arg1, arg2)) = s.as_str().split_once(' ')
19+
&& arg1.starts_with('-')
20+
&& arg1.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
21+
{
22+
span_lint_and_then(
23+
cx,
24+
SUSPICIOUS_COMMAND_ARG_SPACE,
25+
arg.span,
26+
"single argument that looks like it should be multiple arguments",
27+
|diag: &mut Diagnostic| {
28+
diag.multipart_suggestion_verbose(
29+
"consider splitting the argument",
30+
vec![
31+
(span, "args".to_string()),
32+
(arg.span, format!("[{arg1:?}, {arg2:?}]")),
33+
],
34+
Applicability::MaybeIncorrect,
35+
);
36+
}
37+
);
38+
}
39+
}

clippy_utils/src/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub const STD_FS_CREATE_DIR: [&str; 3] = ["std", "fs", "create_dir"];
115115
pub const STD_IO_SEEK: [&str; 3] = ["std", "io", "Seek"];
116116
pub const STD_IO_SEEK_FROM_CURRENT: [&str; 4] = ["std", "io", "SeekFrom", "Current"];
117117
pub const STD_IO_SEEKFROM_START: [&str; 4] = ["std", "io", "SeekFrom", "Start"];
118+
pub const STD_PROCESS_COMMAND: [&str; 3] = ["std", "process", "Command"];
118119
pub const STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_str"];
119120
pub const STRING_AS_STR: [&str; 4] = ["alloc", "string", "String", "as_str"];
120121
pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
// Things it should warn about:
3+
std::process::Command::new("echo").arg("-n hello").spawn().unwrap();
4+
std::process::Command::new("cat").arg("--number file").spawn().unwrap();
5+
6+
// Things it should not warn about:
7+
std::process::Command::new("echo").arg("hello world").spawn().unwrap();
8+
std::process::Command::new("a").arg("--fmt=%a %b %c").spawn().unwrap();
9+
std::process::Command::new("b").arg("-ldflags=-s -w").spawn().unwrap();
10+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: single argument that looks like it should be multiple arguments
2+
--> $DIR/suspicious_command_arg_space.rs:3:44
3+
|
4+
LL | std::process::Command::new("echo").arg("-n hello").spawn().unwrap();
5+
| ^^^^^^^^^^
6+
|
7+
= note: `-D clippy::suspicious-command-arg-space` implied by `-D warnings`
8+
help: consider splitting the argument
9+
|
10+
LL | std::process::Command::new("echo").args(["-n", "hello"]).spawn().unwrap();
11+
| ~~~~ ~~~~~~~~~~~~~~~
12+
13+
error: single argument that looks like it should be multiple arguments
14+
--> $DIR/suspicious_command_arg_space.rs:4:43
15+
|
16+
LL | std::process::Command::new("cat").arg("--number file").spawn().unwrap();
17+
| ^^^^^^^^^^^^^^^
18+
|
19+
help: consider splitting the argument
20+
|
21+
LL | std::process::Command::new("cat").args(["--number", "file"]).spawn().unwrap();
22+
| ~~~~ ~~~~~~~~~~~~~~~~~~~~
23+
24+
error: aborting due to 2 previous errors
25+

0 commit comments

Comments
 (0)