Skip to content

Commit 7eb0216

Browse files
committed
fix: create_dir ignores paths in suggestions
1 parent f2922e7 commit 7eb0216

File tree

4 files changed

+89
-13
lines changed

4 files changed

+89
-13
lines changed

clippy_lints/src/create_dir.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet_with_applicability;
33
use rustc_errors::Applicability;
4-
use rustc_hir::{Expr, ExprKind};
4+
use rustc_hir::{Expr, ExprKind, QPath};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
77
use rustc_span::sym;
@@ -38,6 +38,8 @@ impl LateLintPass<'_> for CreateDir {
3838
&& let ExprKind::Path(ref path) = func.kind
3939
&& let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
4040
&& cx.tcx.is_diagnostic_item(sym::fs_create_dir, def_id)
41+
&& let QPath::Resolved(_, path) = path
42+
&& let Some(last) = path.segments.last()
4143
{
4244
span_lint_and_then(
4345
cx,
@@ -46,13 +48,17 @@ impl LateLintPass<'_> for CreateDir {
4648
"calling `std::fs::create_dir` where there may be a better way",
4749
|diag| {
4850
let mut app = Applicability::MaybeIncorrect;
51+
let prefix_span = func.span.shrink_to_lo().between(last.ident.span);
52+
let prefix_snippet = if !prefix_span.is_empty() {
53+
snippet_with_applicability(cx, prefix_span, "..", &mut app)
54+
} else {
55+
"std::fs::".into()
56+
};
57+
let args_snippet = snippet_with_applicability(cx, arg.span, "..", &mut app);
4958
diag.span_suggestion_verbose(
5059
expr.span,
5160
"consider calling `std::fs::create_dir_all` instead",
52-
format!(
53-
"create_dir_all({})",
54-
snippet_with_applicability(cx, arg.span, "..", &mut app)
55-
),
61+
format!("{prefix_snippet}create_dir_all({args_snippet})",),
5662
app,
5763
);
5864
},

tests/ui/create_dir.fixed

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,31 @@ fn create_dir() {}
77

88
fn main() {
99
// Should be warned
10-
create_dir_all("foo");
10+
std::fs::create_dir_all("foo");
1111
//~^ create_dir
12-
create_dir_all("bar").unwrap();
12+
std::fs::create_dir_all("bar").unwrap();
1313
//~^ create_dir
1414

1515
// Shouldn't be warned
1616
create_dir();
1717
std::fs::create_dir_all("foobar");
1818
}
19+
20+
mod issue14994 {
21+
fn with_no_prefix() {
22+
use std::fs::create_dir;
23+
std::fs::create_dir_all("some/dir").unwrap();
24+
//~^ create_dir
25+
}
26+
27+
fn with_fs_prefix() {
28+
use std::fs;
29+
fs::create_dir_all("/some/dir").unwrap();
30+
//~^ create_dir
31+
}
32+
33+
fn with_full_prefix() {
34+
std::fs::create_dir_all("/some/dir").unwrap();
35+
//~^ create_dir
36+
}
37+
}

tests/ui/create_dir.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,22 @@ fn main() {
1616
create_dir();
1717
std::fs::create_dir_all("foobar");
1818
}
19+
20+
mod issue14994 {
21+
fn with_no_prefix() {
22+
use std::fs::create_dir;
23+
create_dir("some/dir").unwrap();
24+
//~^ create_dir
25+
}
26+
27+
fn with_fs_prefix() {
28+
use std::fs;
29+
fs::create_dir("/some/dir").unwrap();
30+
//~^ create_dir
31+
}
32+
33+
fn with_full_prefix() {
34+
std::fs::create_dir("/some/dir").unwrap();
35+
//~^ create_dir
36+
}
37+
}

tests/ui/create_dir.stderr

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ LL | std::fs::create_dir("foo");
88
= help: to override `-D warnings` add `#[allow(clippy::create_dir)]`
99
help: consider calling `std::fs::create_dir_all` instead
1010
|
11-
LL - std::fs::create_dir("foo");
12-
LL + create_dir_all("foo");
13-
|
11+
LL | std::fs::create_dir_all("foo");
12+
| ++++
1413

1514
error: calling `std::fs::create_dir` where there may be a better way
1615
--> tests/ui/create_dir.rs:12:5
@@ -20,9 +19,42 @@ LL | std::fs::create_dir("bar").unwrap();
2019
|
2120
help: consider calling `std::fs::create_dir_all` instead
2221
|
23-
LL - std::fs::create_dir("bar").unwrap();
24-
LL + create_dir_all("bar").unwrap();
22+
LL | std::fs::create_dir_all("bar").unwrap();
23+
| ++++
24+
25+
error: calling `std::fs::create_dir` where there may be a better way
26+
--> tests/ui/create_dir.rs:23:9
27+
|
28+
LL | create_dir("some/dir").unwrap();
29+
| ^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
help: consider calling `std::fs::create_dir_all` instead
32+
|
33+
LL - create_dir("some/dir").unwrap();
34+
LL + std::fs::create_dir_all("some/dir").unwrap();
35+
|
36+
37+
error: calling `std::fs::create_dir` where there may be a better way
38+
--> tests/ui/create_dir.rs:29:9
39+
|
40+
LL | fs::create_dir("/some/dir").unwrap();
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
|
43+
help: consider calling `std::fs::create_dir_all` instead
44+
|
45+
LL | fs::create_dir_all("/some/dir").unwrap();
46+
| ++++
47+
48+
error: calling `std::fs::create_dir` where there may be a better way
49+
--> tests/ui/create_dir.rs:34:9
50+
|
51+
LL | std::fs::create_dir("/some/dir").unwrap();
52+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
53+
|
54+
help: consider calling `std::fs::create_dir_all` instead
2555
|
56+
LL | std::fs::create_dir_all("/some/dir").unwrap();
57+
| ++++
2658

27-
error: aborting due to 2 previous errors
59+
error: aborting due to 5 previous errors
2860

0 commit comments

Comments
 (0)