2021-03-25 19:29:11 +01:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet;
|
2020-09-10 17:47:07 +02:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir::{Expr, ExprKind};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-11-25 17:45:27 +00:00
|
|
|
use rustc_session::declare_lint_pass;
|
2023-09-26 23:56:38 -04:00
|
|
|
use rustc_span::sym;
|
2020-09-10 17:47:07 +02:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks usage of `std::fs::create_dir` and suggest using `std::fs::create_dir_all` instead.
|
2020-09-10 17:47:07 +02:00
|
|
|
///
|
For restriction lints, replace “Why is this bad?” with “Why restrict this?”
The `restriction` group contains many lints which are not about
necessarily “bad” things, but style choices — perhaps even style choices
which contradict conventional Rust style — or are otherwise very
situational. This results in silly wording like “Why is this bad?
It isn't, but ...”, which I’ve seen confuse a newcomer at least once.
To improve this situation, this commit replaces the “Why is this bad?”
section heading with “Why restrict this?”, for most, but not all,
restriction lints. I left alone the ones whose placement in the
restriction group is more incidental.
In order to make this make sense, I had to remove the “It isn't, but”
texts from the contents of the sections. Sometimes further changes
were needed, or there were obvious fixes to make, and I went ahead
and made those changes without attempting to split them into another
commit, even though many of them are not strictly necessary for the
“Why restrict this?” project.
2024-05-22 22:21:01 -07:00
|
|
|
/// ### Why restrict this?
|
|
|
|
/// Sometimes `std::fs::create_dir` is mistakenly chosen over `std::fs::create_dir_all`,
|
|
|
|
/// resulting in failure when more than one directory needs to be created or when the directory already exists.
|
|
|
|
/// Crates which never need to specifically create a single directory may wish to prevent this mistake.
|
2020-09-10 17:47:07 +02:00
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Example
|
2022-06-16 17:39:06 +02:00
|
|
|
/// ```rust,ignore
|
2020-09-10 17:47:07 +02:00
|
|
|
/// std::fs::create_dir("foo");
|
|
|
|
/// ```
|
2022-06-16 17:39:06 +02:00
|
|
|
///
|
2020-09-10 17:47:07 +02:00
|
|
|
/// Use instead:
|
2022-06-16 17:39:06 +02:00
|
|
|
/// ```rust,ignore
|
2020-09-10 17:47:07 +02:00
|
|
|
/// std::fs::create_dir_all("foo");
|
|
|
|
/// ```
|
2021-12-06 12:33:31 +01:00
|
|
|
#[clippy::version = "1.48.0"]
|
2020-09-10 17:47:07 +02:00
|
|
|
pub CREATE_DIR,
|
|
|
|
restriction,
|
|
|
|
"calling `std::fs::create_dir` instead of `std::fs::create_dir_all`"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(CreateDir => [CREATE_DIR]);
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for CreateDir {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
2023-11-10 17:29:28 +00:00
|
|
|
if let ExprKind::Call(func, [arg, ..]) = expr.kind
|
|
|
|
&& let ExprKind::Path(ref path) = func.kind
|
|
|
|
&& let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
|
|
|
|
&& cx.tcx.is_diagnostic_item(sym::fs_create_dir, def_id)
|
|
|
|
{
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
CREATE_DIR,
|
|
|
|
expr.span,
|
|
|
|
"calling `std::fs::create_dir` where there may be a better way",
|
|
|
|
"consider calling `std::fs::create_dir_all` instead",
|
|
|
|
format!("create_dir_all({})", snippet(cx, arg.span, "..")),
|
|
|
|
Applicability::MaybeIncorrect,
|
2023-11-02 19:23:36 +00:00
|
|
|
);
|
2020-09-10 17:47:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|