Skip to content

Commit 5034d47

Browse files
committed
Auto merge of #5980 - matsujika:create-dir, r=flip1995
Add a lint to prevent `create_dir` from being used This closes #5950 changelog: none
2 parents 55efa96 + a899ad2 commit 5034d47

File tree

8 files changed

+114
-0
lines changed

8 files changed

+114
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,7 @@ Released 2018-09-13
15471547
[`collapsible_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
15481548
[`comparison_chain`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
15491549
[`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator
1550+
[`create_dir`]: https://rust-lang.github.io/rust-clippy/master/index.html#create_dir
15501551
[`crosspointer_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#crosspointer_transmute
15511552
[`dbg_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro
15521553
[`debug_assert_with_mut_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#debug_assert_with_mut_call

clippy_lints/src/create_dir.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate::utils::{match_def_path, paths, snippet, span_lint_and_sugg};
2+
use if_chain::if_chain;
3+
use rustc_errors::Applicability;
4+
use rustc_hir::{Expr, ExprKind};
5+
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
8+
declare_clippy_lint! {
9+
/// **What it does:** Checks usage of `std::fs::create_dir` and suggest using `std::fs::create_dir_all` instead.
10+
///
11+
/// **Why is this bad?** Sometimes `std::fs::crate_dir` is mistakenly chosen over `std::fs::create_dir_all`.
12+
///
13+
/// **Known problems:** None.
14+
///
15+
/// **Example:**
16+
///
17+
/// ```rust
18+
/// std::fs::create_dir("foo");
19+
/// ```
20+
/// Use instead:
21+
/// ```rust
22+
/// std::fs::create_dir_all("foo");
23+
/// ```
24+
pub CREATE_DIR,
25+
restriction,
26+
"calling `std::fs::create_dir` instead of `std::fs::create_dir_all`"
27+
}
28+
29+
declare_lint_pass!(CreateDir => [CREATE_DIR]);
30+
31+
impl LateLintPass<'_> for CreateDir {
32+
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
33+
if_chain! {
34+
if let ExprKind::Call(ref func, ref args) = expr.kind;
35+
if let ExprKind::Path(ref path) = func.kind;
36+
if let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id();
37+
if match_def_path(cx, def_id, &paths::STD_FS_CREATE_DIR);
38+
then {
39+
span_lint_and_sugg(
40+
cx,
41+
CREATE_DIR,
42+
expr.span,
43+
"calling `std::fs::create_dir` where there may be a better way",
44+
"consider calling `std::fs::create_dir_all` instead",
45+
format!("create_dir_all({})", snippet(cx, args[0].span, "..")),
46+
Applicability::MaybeIncorrect,
47+
)
48+
}
49+
}
50+
}
51+
}

clippy_lints/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ mod collapsible_if;
170170
mod comparison_chain;
171171
mod copies;
172172
mod copy_iterator;
173+
mod create_dir;
173174
mod dbg_macro;
174175
mod default_trait_access;
175176
mod dereference;
@@ -513,6 +514,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
513514
&copies::MATCH_SAME_ARMS,
514515
&copies::SAME_FUNCTIONS_IN_IF_CONDITION,
515516
&copy_iterator::COPY_ITERATOR,
517+
&create_dir::CREATE_DIR,
516518
&dbg_macro::DBG_MACRO,
517519
&default_trait_access::DEFAULT_TRAIT_ACCESS,
518520
&dereference::EXPLICIT_DEREF_METHODS,
@@ -1044,6 +1046,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10441046
store.register_early_pass(|| box items_after_statements::ItemsAfterStatements);
10451047
store.register_early_pass(|| box precedence::Precedence);
10461048
store.register_early_pass(|| box needless_continue::NeedlessContinue);
1049+
store.register_late_pass(|| box create_dir::CreateDir);
10471050
store.register_early_pass(|| box needless_arbitrary_self_type::NeedlessArbitrarySelfType);
10481051
store.register_early_pass(|| box redundant_static_lifetimes::RedundantStaticLifetimes);
10491052
store.register_late_pass(|| box cargo_common_metadata::CargoCommonMetadata);
@@ -1107,6 +1110,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11071110
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
11081111
LintId::of(&arithmetic::INTEGER_ARITHMETIC),
11091112
LintId::of(&as_conversions::AS_CONVERSIONS),
1113+
LintId::of(&create_dir::CREATE_DIR),
11101114
LintId::of(&dbg_macro::DBG_MACRO),
11111115
LintId::of(&else_if_without_else::ELSE_IF_WITHOUT_ELSE),
11121116
LintId::of(&exit::EXIT),

clippy_lints/src/utils/paths.rs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub const SLICE_ITER: [&str; 3] = ["core", "slice", "Iter"];
110110
pub const STDERR: [&str; 4] = ["std", "io", "stdio", "stderr"];
111111
pub const STDOUT: [&str; 4] = ["std", "io", "stdio", "stdout"];
112112
pub const STD_CONVERT_IDENTITY: [&str; 3] = ["std", "convert", "identity"];
113+
pub const STD_FS_CREATE_DIR: [&str; 3] = ["std", "fs", "create_dir"];
113114
pub const STD_MEM_TRANSMUTE: [&str; 3] = ["std", "mem", "transmute"];
114115
pub const STD_PTR_NULL: [&str; 3] = ["std", "ptr", "null"];
115116
pub const STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_str"];

src/lintlist/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
297297
deprecation: None,
298298
module: "copy_iterator",
299299
},
300+
Lint {
301+
name: "create_dir",
302+
group: "restriction",
303+
desc: "calling `std::fs::create_dir` instead of `std::fs::create_dir_all`",
304+
deprecation: None,
305+
module: "create_dir",
306+
},
300307
Lint {
301308
name: "crosspointer_transmute",
302309
group: "complexity",

tests/ui/create_dir.fixed

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-rustfix
2+
#![allow(unused_must_use)]
3+
#![warn(clippy::create_dir)]
4+
5+
use std::fs::create_dir_all;
6+
7+
fn create_dir() {}
8+
9+
fn main() {
10+
// Should be warned
11+
create_dir_all("foo");
12+
create_dir_all("bar").unwrap();
13+
14+
// Shouldn't be warned
15+
create_dir();
16+
std::fs::create_dir_all("foobar");
17+
}

tests/ui/create_dir.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-rustfix
2+
#![allow(unused_must_use)]
3+
#![warn(clippy::create_dir)]
4+
5+
use std::fs::create_dir_all;
6+
7+
fn create_dir() {}
8+
9+
fn main() {
10+
// Should be warned
11+
std::fs::create_dir("foo");
12+
std::fs::create_dir("bar").unwrap();
13+
14+
// Shouldn't be warned
15+
create_dir();
16+
std::fs::create_dir_all("foobar");
17+
}

tests/ui/create_dir.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: calling `std::fs::create_dir` where there may be a better way
2+
--> $DIR/create_dir.rs:11:5
3+
|
4+
LL | std::fs::create_dir("foo");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `std::fs::create_dir_all` instead: `create_dir_all("foo")`
6+
|
7+
= note: `-D clippy::create-dir` implied by `-D warnings`
8+
9+
error: calling `std::fs::create_dir` where there may be a better way
10+
--> $DIR/create_dir.rs:12:5
11+
|
12+
LL | std::fs::create_dir("bar").unwrap();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `std::fs::create_dir_all` instead: `create_dir_all("bar")`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)