Use match_def_path instead of match_qpath

This commit is contained in:
Hirochika Matsumoto 2020-08-31 22:40:47 +09:00
parent 5b7590f841
commit 451ef78803
4 changed files with 13 additions and 8 deletions

View File

@ -1,4 +1,4 @@
use crate::utils::{match_qpath, paths, snippet, span_lint_and_sugg};
use crate::utils::{match_def_path, paths, snippet, span_lint_and_sugg};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
@ -33,7 +33,8 @@ impl LateLintPass<'_> for CreateDir {
if_chain! {
if let ExprKind::Call(ref func, ref args) = expr.kind;
if let ExprKind::Path(ref path) = func.kind;
if match_qpath(path, &paths::STD_FS_CREATE_DIR);
if let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id();
if match_def_path(cx, def_id, &paths::STD_FS_CREATE_DIR);
then {
span_lint_and_sugg(
cx,

View File

@ -2,12 +2,14 @@
#![allow(unused_must_use)]
#![warn(clippy::create_dir)]
fn not_create_dir() {}
fn create_dir() {}
fn main() {
// Should be warned
std::fs::create_dir_all("foo");
std::fs::create_dir_all("bar").unwrap();
not_create_dir();
// Shouldn't be warned
create_dir();
std::fs::create_dir_all("foobar");
}

View File

@ -2,12 +2,14 @@
#![allow(unused_must_use)]
#![warn(clippy::create_dir)]
fn not_create_dir() {}
fn create_dir() {}
fn main() {
// Should be warned
std::fs::create_dir("foo");
std::fs::create_dir("bar").unwrap();
not_create_dir();
// Shouldn't be warned
create_dir();
std::fs::create_dir_all("foobar");
}

View File

@ -1,5 +1,5 @@
error: calling `std::fs::create_dir` where there may be a better way
--> $DIR/create_dir.rs:8:5
--> $DIR/create_dir.rs:9:5
|
LL | std::fs::create_dir("foo");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `std::fs::create_dir_all` instead: `std::fs::create_dir_all("foo")`
@ -7,7 +7,7 @@ LL | std::fs::create_dir("foo");
= note: `-D clippy::create-dir` implied by `-D warnings`
error: calling `std::fs::create_dir` where there may be a better way
--> $DIR/create_dir.rs:9:5
--> $DIR/create_dir.rs:10:5
|
LL | std::fs::create_dir("bar").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider calling `std::fs::create_dir_all` instead: `std::fs::create_dir_all("bar")`