1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::snippet;
3 use clippy_utils::{match_def_path, paths};
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
6 use rustc_hir::{Expr, ExprKind};
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
12 /// Checks usage of `std::fs::create_dir` and suggest using `std::fs::create_dir_all` instead.
14 /// ### Why is this bad?
15 /// Sometimes `std::fs::create_dir` is mistakenly chosen over `std::fs::create_dir_all`.
20 /// std::fs::create_dir("foo");
24 /// std::fs::create_dir_all("foo");
26 #[clippy::version = "1.48.0"]
29 "calling `std::fs::create_dir` instead of `std::fs::create_dir_all`"
32 declare_lint_pass!(CreateDir => [CREATE_DIR]);
34 impl LateLintPass<'_> for CreateDir {
35 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
37 if let ExprKind::Call(func, args) = expr.kind;
38 if let ExprKind::Path(ref path) = func.kind;
39 if let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id();
40 if match_def_path(cx, def_id, &paths::STD_FS_CREATE_DIR);
46 "calling `std::fs::create_dir` where there may be a better way",
47 "consider calling `std::fs::create_dir_all` instead",
48 format!("create_dir_all({})", snippet(cx, args[0].span, "..")),
49 Applicability::MaybeIncorrect,