]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/create_dir.rs
bump version to 1.80.1+dfsg1-1~bpo12+pve1
[rustc.git] / src / tools / clippy / clippy_lints / src / create_dir.rs
CommitLineData
cdc7bbd5
XL
1use clippy_utils::diagnostics::span_lint_and_sugg;
2use clippy_utils::source::snippet;
f20569fa
XL
3use rustc_errors::Applicability;
4use rustc_hir::{Expr, ExprKind};
5use rustc_lint::{LateContext, LateLintPass};
4b012472 6use rustc_session::declare_lint_pass;
ed00b5ec 7use rustc_span::sym;
f20569fa
XL
8
9declare_clippy_lint! {
94222f64
XL
10 /// ### What it does
11 /// Checks usage of `std::fs::create_dir` and suggest using `std::fs::create_dir_all` instead.
f20569fa 12 ///
31ef2f64
FG
13 /// ### Why restrict this?
14 /// Sometimes `std::fs::create_dir` is mistakenly chosen over `std::fs::create_dir_all`,
15 /// resulting in failure when more than one directory needs to be created or when the directory already exists.
16 /// Crates which never need to specifically create a single directory may wish to prevent this mistake.
f20569fa 17 ///
94222f64 18 /// ### Example
923072b8 19 /// ```rust,ignore
f20569fa
XL
20 /// std::fs::create_dir("foo");
21 /// ```
923072b8 22 ///
f20569fa 23 /// Use instead:
923072b8 24 /// ```rust,ignore
f20569fa
XL
25 /// std::fs::create_dir_all("foo");
26 /// ```
a2a8927a 27 #[clippy::version = "1.48.0"]
f20569fa
XL
28 pub CREATE_DIR,
29 restriction,
30 "calling `std::fs::create_dir` instead of `std::fs::create_dir_all`"
31}
32
33declare_lint_pass!(CreateDir => [CREATE_DIR]);
34
35impl LateLintPass<'_> for CreateDir {
36 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
4b012472
FG
37 if let ExprKind::Call(func, [arg, ..]) = expr.kind
38 && let ExprKind::Path(ref path) = func.kind
39 && let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
40 && cx.tcx.is_diagnostic_item(sym::fs_create_dir, def_id)
41 {
42 span_lint_and_sugg(
43 cx,
44 CREATE_DIR,
45 expr.span,
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, arg.span, "..")),
49 Applicability::MaybeIncorrect,
50 );
f20569fa
XL
51 }
52 }
53}