-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add suspicious_command_arg_space
lint
#10317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
145e6a9
Add suspicious_command_arg_space lint.
m-ou-se 5fefe8b
Add test.
m-ou-se 984c47b
Clarify description of suspicious_command_arg_space.
m-ou-se 8f56767
Update lints.
m-ou-se 805a0ae
Add more test cases for suspicious_command_arg_space.
m-ou-se 1f77866
Add SUSPICIOUS_COMMAND_ARG_SPACE to lint pass.
m-ou-se File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::paths; | ||
use clippy_utils::ty::match_type; | ||
use rustc_ast as ast; | ||
use rustc_errors::{Applicability, Diagnostic}; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::Span; | ||
|
||
use super::SUSPICIOUS_COMMAND_ARG_SPACE; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, recv: &'tcx hir::Expr<'_>, arg: &'tcx hir::Expr<'_>, span: Span) { | ||
let ty = cx.typeck_results().expr_ty(recv).peel_refs(); | ||
|
||
if match_type(cx, ty, &paths::STD_PROCESS_COMMAND) | ||
&& let hir::ExprKind::Lit(lit) = &arg.kind | ||
&& let ast::LitKind::Str(s, _) = &lit.node | ||
&& let Some((arg1, arg2)) = s.as_str().split_once(' ') | ||
&& arg1.starts_with('-') | ||
&& arg1.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') | ||
{ | ||
span_lint_and_then( | ||
cx, | ||
SUSPICIOUS_COMMAND_ARG_SPACE, | ||
arg.span, | ||
"single argument that looks like it should be multiple arguments", | ||
|diag: &mut Diagnostic| { | ||
diag.multipart_suggestion_verbose( | ||
"consider splitting the argument", | ||
vec![ | ||
(span, "args".to_string()), | ||
(arg.span, format!("[{arg1:?}, {arg2:?}]")), | ||
], | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
fn main() { | ||
// Things it should warn about: | ||
std::process::Command::new("echo").arg("-n hello").spawn().unwrap(); | ||
std::process::Command::new("cat").arg("--number file").spawn().unwrap(); | ||
|
||
// Things it should not warn about: | ||
std::process::Command::new("echo").arg("hello world").spawn().unwrap(); | ||
std::process::Command::new("a").arg("--fmt=%a %b %c").spawn().unwrap(); | ||
std::process::Command::new("b").arg("-ldflags=-s -w").spawn().unwrap(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error: single argument that looks like it should be multiple arguments | ||
--> $DIR/suspicious_command_arg_space.rs:3:44 | ||
| | ||
LL | std::process::Command::new("echo").arg("-n hello").spawn().unwrap(); | ||
| ^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::suspicious-command-arg-space` implied by `-D warnings` | ||
help: consider splitting the argument | ||
| | ||
LL | std::process::Command::new("echo").args(["-n", "hello"]).spawn().unwrap(); | ||
| ~~~~ ~~~~~~~~~~~~~~~ | ||
|
||
error: single argument that looks like it should be multiple arguments | ||
--> $DIR/suspicious_command_arg_space.rs:4:43 | ||
| | ||
LL | std::process::Command::new("cat").arg("--number file").spawn().unwrap(); | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
help: consider splitting the argument | ||
| | ||
LL | std::process::Command::new("cat").args(["--number", "file"]).spawn().unwrap(); | ||
| ~~~~ ~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.