|
| 1 | +use std::borrow::Cow; |
| 2 | +use std::collections::BTreeMap; |
| 3 | + |
| 4 | +use clippy_config::Conf; |
| 5 | +use clippy_utils::consts::{ConstEvalCtxt, Constant}; |
| 6 | +use clippy_utils::diagnostics::{span_lint, span_lint_and_help}; |
| 7 | +use clippy_utils::path_def_id; |
| 8 | +use clippy_utils::ty::get_adt_inherent_method; |
| 9 | +use rustc_hir::def_id::DefId; |
| 10 | +use rustc_hir::{Expr, ExprKind}; |
| 11 | +use rustc_lint::{LateContext, LateLintPass}; |
| 12 | +use rustc_middle::ty::{List, Ty}; |
| 13 | +use rustc_session::impl_lint_pass; |
| 14 | +use rustc_span::sym; |
| 15 | + |
| 16 | +declare_clippy_lint! { |
| 17 | + /// ### What it does |
| 18 | + /// |
| 19 | + /// Detects calls to `std::process::Command::new` that can easily be replaced with Rust code. |
| 20 | + /// |
| 21 | + /// ### Why is this bad? |
| 22 | + /// |
| 23 | + /// "Shelling out" is slow, non-portable, and generally unnecessary (especially to these programs). |
| 24 | + /// |
| 25 | + /// ### Example |
| 26 | + /// ```no_run |
| 27 | + /// use std::io; |
| 28 | + /// use std::process::Command; |
| 29 | + /// |
| 30 | + /// fn list_files() -> io::Result<Vec<String>> { |
| 31 | + /// let output = Command::new("ls").output()?; |
| 32 | + /// if !output.success() { |
| 33 | + /// return Err(io::Error::new( |
| 34 | + /// io::ErrorKind::Other, |
| 35 | + /// output.stderr.to_string_lossy()) |
| 36 | + /// ); |
| 37 | + /// } |
| 38 | + /// |
| 39 | + /// let stdout = std::str::from_utf8(&output.stdout)?; |
| 40 | + /// Ok(stdout.split_whitespace().map(String::from).collect()) |
| 41 | + /// } |
| 42 | + /// let folders |
| 43 | + /// // example code where clippy issues a warning |
| 44 | + /// ``` |
| 45 | + /// Use instead: |
| 46 | + /// ```no_run |
| 47 | + /// fn list_files() -> std::io::Result<Vec<String>> { |
| 48 | + /// let mut buf = Vec::new(); |
| 49 | + /// for entry_res in std::fs::read_dir(".")? { |
| 50 | + /// let path = entry_res?.path(); |
| 51 | + /// let os_name = path.into_os_string(); |
| 52 | + /// buf.push(os_name.into_string().expect("should be UTF-8 paths")) |
| 53 | + /// } |
| 54 | + /// } |
| 55 | + /// ``` |
| 56 | + #[clippy::version = "1.84.0"] |
| 57 | + pub UNNECESSARY_SHELL_COMMAND, |
| 58 | + pedantic, |
| 59 | + "using the simple shell utilities instead of Rust code" |
| 60 | +} |
| 61 | + |
| 62 | +pub struct UnnecessaryShellCommand { |
| 63 | + std_process_command_new: Option<DefId>, |
| 64 | + unnecessary_commands: &'static BTreeMap<Box<str>, Option<Cow<'static, str>>>, |
| 65 | +} |
| 66 | + |
| 67 | +impl UnnecessaryShellCommand { |
| 68 | + pub fn new(conf: &'static Conf) -> Self { |
| 69 | + Self { |
| 70 | + std_process_command_new: None, |
| 71 | + unnecessary_commands: &conf.unnecessary_commands, |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl_lint_pass!(UnnecessaryShellCommand => [UNNECESSARY_SHELL_COMMAND]); |
| 77 | + |
| 78 | +impl LateLintPass<'_> for UnnecessaryShellCommand { |
| 79 | + fn check_crate(&mut self, cx: &LateContext<'_>) { |
| 80 | + if let Some(command_did) = cx.tcx.get_diagnostic_item(sym::Command) { |
| 81 | + let process_ty = Ty::new_adt(cx.tcx, cx.tcx.adt_def(command_did), List::empty()); |
| 82 | + if let Some(fn_item) = get_adt_inherent_method(cx, process_ty, sym::new) { |
| 83 | + self.std_process_command_new = Some(fn_item.def_id); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 89 | + let Some(std_process_command_new) = self.std_process_command_new else { |
| 90 | + return; |
| 91 | + }; |
| 92 | + |
| 93 | + if let ExprKind::Call(func, [command]) = expr.kind |
| 94 | + && path_def_id(cx, func) == Some(std_process_command_new) |
| 95 | + && let Some(Constant::Str(command_lit)) = ConstEvalCtxt::new(cx).eval(command) |
| 96 | + && let command_lit = command_lit.strip_suffix(".exe").unwrap_or(&command_lit) |
| 97 | + && let Some(help) = self.unnecessary_commands.get(command_lit) |
| 98 | + { |
| 99 | + let lint = UNNECESSARY_SHELL_COMMAND; |
| 100 | + let msg = "unnecessarily shelling out for trivial operation"; |
| 101 | + if let Some(help) = help.as_deref() { |
| 102 | + span_lint_and_help(cx, lint, command.span, msg, None, help); |
| 103 | + } else { |
| 104 | + span_lint(cx, lint, command.span, msg); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments