Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions clippy_lints/src/create_dir.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_with_applicability;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
use rustc_span::sym;
Expand Down Expand Up @@ -34,26 +33,28 @@ declare_lint_pass!(CreateDir => [CREATE_DIR]);

impl LateLintPass<'_> for CreateDir {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
if let ExprKind::Call(func, [arg]) = expr.kind
if let ExprKind::Call(func, [_]) = 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)
&& let QPath::Resolved(_, path) = path
&& let Some(last) = path.segments.last()
{
span_lint_and_then(
cx,
CREATE_DIR,
expr.span,
"calling `std::fs::create_dir` where there may be a better way",
|diag| {
let mut app = Applicability::MaybeIncorrect;
diag.span_suggestion_verbose(
expr.span,
let mut suggestions = vec![(last.ident.span.shrink_to_hi(), "_all".to_owned())];
if path.segments.len() == 1 {
suggestions.push((path.span.shrink_to_lo(), "std::fs::".to_owned()));
}

diag.multipart_suggestion_verbose(
"consider calling `std::fs::create_dir_all` instead",
format!(
"create_dir_all({})",
snippet_with_applicability(cx, arg.span, "..", &mut app)
),
app,
suggestions,
Applicability::MaybeIncorrect,
);
},
);
Expand Down
23 changes: 21 additions & 2 deletions tests/ui/create_dir.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,31 @@ fn create_dir() {}

fn main() {
// Should be warned
create_dir_all("foo");
std::fs::create_dir_all("foo");
//~^ create_dir
create_dir_all("bar").unwrap();
std::fs::create_dir_all("bar").unwrap();
//~^ create_dir

// Shouldn't be warned
create_dir();
std::fs::create_dir_all("foobar");
}

mod issue14994 {
fn with_no_prefix() {
use std::fs::create_dir;
std::fs::create_dir_all("some/dir").unwrap();
//~^ create_dir
}

fn with_fs_prefix() {
use std::fs;
fs::create_dir_all("/some/dir").unwrap();
//~^ create_dir
}

fn with_full_prefix() {
std::fs::create_dir_all("/some/dir").unwrap();
//~^ create_dir
}
}
19 changes: 19 additions & 0 deletions tests/ui/create_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,22 @@ fn main() {
create_dir();
std::fs::create_dir_all("foobar");
}

mod issue14994 {
fn with_no_prefix() {
use std::fs::create_dir;
create_dir("some/dir").unwrap();
//~^ create_dir
}

fn with_fs_prefix() {
use std::fs;
fs::create_dir("/some/dir").unwrap();
//~^ create_dir
}

fn with_full_prefix() {
std::fs::create_dir("/some/dir").unwrap();
//~^ create_dir
}
}
43 changes: 37 additions & 6 deletions tests/ui/create_dir.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ LL | std::fs::create_dir("foo");
= help: to override `-D warnings` add `#[allow(clippy::create_dir)]`
help: consider calling `std::fs::create_dir_all` instead
|
LL - std::fs::create_dir("foo");
LL + create_dir_all("foo");
|
LL | std::fs::create_dir_all("foo");
| ++++

error: calling `std::fs::create_dir` where there may be a better way
--> tests/ui/create_dir.rs:12:5
Expand All @@ -20,9 +19,41 @@ LL | std::fs::create_dir("bar").unwrap();
|
help: consider calling `std::fs::create_dir_all` instead
|
LL - std::fs::create_dir("bar").unwrap();
LL + create_dir_all("bar").unwrap();
LL | std::fs::create_dir_all("bar").unwrap();
| ++++

error: calling `std::fs::create_dir` where there may be a better way
--> tests/ui/create_dir.rs:23:9
|
LL | create_dir("some/dir").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: consider calling `std::fs::create_dir_all` instead
|
LL | std::fs::create_dir_all("some/dir").unwrap();
| +++++++++ ++++

error: calling `std::fs::create_dir` where there may be a better way
--> tests/ui/create_dir.rs:29:9
|
LL | fs::create_dir("/some/dir").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider calling `std::fs::create_dir_all` instead
|
LL | fs::create_dir_all("/some/dir").unwrap();
| ++++

error: calling `std::fs::create_dir` where there may be a better way
--> tests/ui/create_dir.rs:34:9
|
LL | std::fs::create_dir("/some/dir").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider calling `std::fs::create_dir_all` instead
|
LL | std::fs::create_dir_all("/some/dir").unwrap();
| ++++

error: aborting due to 2 previous errors
error: aborting due to 5 previous errors