Skip to content

Commit 8623b33

Browse files
committed
move filetype_is_file to its own module
1 parent 60a0537 commit 8623b33

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::utils::{get_parent_expr, match_type, paths, span_lint_and_help};
2+
use if_chain::if_chain;
3+
use rustc_hir as hir;
4+
use rustc_lint::LateContext;
5+
use rustc_span::source_map::Span;
6+
7+
use super::FILETYPE_IS_FILE;
8+
9+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
10+
let ty = cx.typeck_results().expr_ty(&args[0]);
11+
12+
if !match_type(cx, ty, &paths::FILE_TYPE) {
13+
return;
14+
}
15+
16+
let span: Span;
17+
let verb: &str;
18+
let lint_unary: &str;
19+
let help_unary: &str;
20+
if_chain! {
21+
if let Some(parent) = get_parent_expr(cx, expr);
22+
if let hir::ExprKind::Unary(op, _) = parent.kind;
23+
if op == hir::UnOp::Not;
24+
then {
25+
lint_unary = "!";
26+
verb = "denies";
27+
help_unary = "";
28+
span = parent.span;
29+
} else {
30+
lint_unary = "";
31+
verb = "covers";
32+
help_unary = "!";
33+
span = expr.span;
34+
}
35+
}
36+
let lint_msg = format!("`{}FileType::is_file()` only {} regular files", lint_unary, verb);
37+
let help_msg = format!("use `{}FileType::is_dir()` instead", help_unary);
38+
span_lint_and_help(cx, FILETYPE_IS_FILE, span, &lint_msg, None, &help_msg);
39+
}

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod bind_instead_of_map;
22
mod bytes_nth;
33
mod expect_used;
4+
mod filetype_is_file;
45
mod filter_map_identity;
56
mod filter_next;
67
mod get_unwrap;
@@ -1725,7 +1726,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17251726
["add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub"] => {
17261727
check_pointer_offset(cx, expr, arg_lists[0])
17271728
},
1728-
["is_file", ..] => lint_filetype_is_file(cx, expr, arg_lists[0]),
1729+
["is_file", ..] => filetype_is_file::check(cx, expr, arg_lists[0]),
17291730
["map", "as_ref"] => {
17301731
option_as_ref_deref::check(cx, expr, arg_lists[1], arg_lists[0], false, self.msrv.as_ref())
17311732
},
@@ -3859,38 +3860,6 @@ fn check_pointer_offset(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir:
38593860
}
38603861
}
38613862

3862-
fn lint_filetype_is_file(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
3863-
let ty = cx.typeck_results().expr_ty(&args[0]);
3864-
3865-
if !match_type(cx, ty, &paths::FILE_TYPE) {
3866-
return;
3867-
}
3868-
3869-
let span: Span;
3870-
let verb: &str;
3871-
let lint_unary: &str;
3872-
let help_unary: &str;
3873-
if_chain! {
3874-
if let Some(parent) = get_parent_expr(cx, expr);
3875-
if let hir::ExprKind::Unary(op, _) = parent.kind;
3876-
if op == hir::UnOp::Not;
3877-
then {
3878-
lint_unary = "!";
3879-
verb = "denies";
3880-
help_unary = "";
3881-
span = parent.span;
3882-
} else {
3883-
lint_unary = "";
3884-
verb = "covers";
3885-
help_unary = "!";
3886-
span = expr.span;
3887-
}
3888-
}
3889-
let lint_msg = format!("`{}FileType::is_file()` only {} regular files", lint_unary, verb);
3890-
let help_msg = format!("use `{}FileType::is_dir()` instead", help_unary);
3891-
span_lint_and_help(cx, FILETYPE_IS_FILE, span, &lint_msg, None, &help_msg);
3892-
}
3893-
38943863
fn lint_from_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
38953864
let ty = cx.typeck_results().expr_ty(expr);
38963865
let arg_ty = cx.typeck_results().expr_ty(&args[0]);

0 commit comments

Comments
 (0)