Skip to content

Commit 3af333e

Browse files
authored
Fix create_dir ignores paths in suggestions (#15011)
Closes rust-lang/rust-clippy#14994 changelog: [`create_dir`] fix missing paths in suggestions
2 parents 737b8be + 4d6254c commit 3af333e

File tree

4 files changed

+89
-19
lines changed

4 files changed

+89
-19
lines changed

clippy_lints/src/create_dir.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::source::snippet_with_applicability;
32
use rustc_errors::Applicability;
4-
use rustc_hir::{Expr, ExprKind};
3+
use rustc_hir::{Expr, ExprKind, QPath};
54
use rustc_lint::{LateContext, LateLintPass};
65
use rustc_session::declare_lint_pass;
76
use rustc_span::sym;
@@ -34,26 +33,28 @@ declare_lint_pass!(CreateDir => [CREATE_DIR]);
3433

3534
impl LateLintPass<'_> for CreateDir {
3635
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
37-
if let ExprKind::Call(func, [arg]) = expr.kind
36+
if let ExprKind::Call(func, [_]) = expr.kind
3837
&& let ExprKind::Path(ref path) = func.kind
3938
&& let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
4039
&& cx.tcx.is_diagnostic_item(sym::fs_create_dir, def_id)
40+
&& let QPath::Resolved(_, path) = path
41+
&& let Some(last) = path.segments.last()
4142
{
4243
span_lint_and_then(
4344
cx,
4445
CREATE_DIR,
4546
expr.span,
4647
"calling `std::fs::create_dir` where there may be a better way",
4748
|diag| {
48-
let mut app = Applicability::MaybeIncorrect;
49-
diag.span_suggestion_verbose(
50-
expr.span,
49+
let mut suggestions = vec![(last.ident.span.shrink_to_hi(), "_all".to_owned())];
50+
if path.segments.len() == 1 {
51+
suggestions.push((path.span.shrink_to_lo(), "std::fs::".to_owned()));
52+
}
53+
54+
diag.multipart_suggestion_verbose(
5155
"consider calling `std::fs::create_dir_all` instead",
52-
format!(
53-
"create_dir_all({})",
54-
snippet_with_applicability(cx, arg.span, "..", &mut app)
55-
),
56-
app,
56+
suggestions,
57+
Applicability::MaybeIncorrect,
5758
);
5859
},
5960
);

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: 37 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,41 @@ 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 | std::fs::create_dir_all("some/dir").unwrap();
34+
| +++++++++ ++++
35+
36+
error: calling `std::fs::create_dir` where there may be a better way
37+
--> tests/ui/create_dir.rs:29:9
38+
|
39+
LL | fs::create_dir("/some/dir").unwrap();
40+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
|
42+
help: consider calling `std::fs::create_dir_all` instead
43+
|
44+
LL | fs::create_dir_all("/some/dir").unwrap();
45+
| ++++
46+
47+
error: calling `std::fs::create_dir` where there may be a better way
48+
--> tests/ui/create_dir.rs:34:9
49+
|
50+
LL | std::fs::create_dir("/some/dir").unwrap();
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
|
53+
help: consider calling `std::fs::create_dir_all` instead
2554
|
55+
LL | std::fs::create_dir_all("/some/dir").unwrap();
56+
| ++++
2657

27-
error: aborting due to 2 previous errors
58+
error: aborting due to 5 previous errors
2859

0 commit comments

Comments
 (0)