Skip to content

Commit

Permalink
move filetype_is_file to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
TaKO8Ki committed Mar 11, 2021
1 parent 60a0537 commit 8623b33
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
39 changes: 39 additions & 0 deletions clippy_lints/src/methods/filetype_is_file.rs
@@ -0,0 +1,39 @@
use crate::utils::{get_parent_expr, match_type, paths, span_lint_and_help};
use if_chain::if_chain;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::source_map::Span;

use super::FILETYPE_IS_FILE;

pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
let ty = cx.typeck_results().expr_ty(&args[0]);

if !match_type(cx, ty, &paths::FILE_TYPE) {
return;
}

let span: Span;
let verb: &str;
let lint_unary: &str;
let help_unary: &str;
if_chain! {
if let Some(parent) = get_parent_expr(cx, expr);
if let hir::ExprKind::Unary(op, _) = parent.kind;
if op == hir::UnOp::Not;
then {
lint_unary = "!";
verb = "denies";
help_unary = "";
span = parent.span;
} else {
lint_unary = "";
verb = "covers";
help_unary = "!";
span = expr.span;
}
}
let lint_msg = format!("`{}FileType::is_file()` only {} regular files", lint_unary, verb);
let help_msg = format!("use `{}FileType::is_dir()` instead", help_unary);
span_lint_and_help(cx, FILETYPE_IS_FILE, span, &lint_msg, None, &help_msg);
}
35 changes: 2 additions & 33 deletions clippy_lints/src/methods/mod.rs
@@ -1,6 +1,7 @@
mod bind_instead_of_map;
mod bytes_nth;
mod expect_used;
mod filetype_is_file;
mod filter_map_identity;
mod filter_next;
mod get_unwrap;
Expand Down Expand Up @@ -1725,7 +1726,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
["add" | "offset" | "sub" | "wrapping_offset" | "wrapping_add" | "wrapping_sub"] => {
check_pointer_offset(cx, expr, arg_lists[0])
},
["is_file", ..] => lint_filetype_is_file(cx, expr, arg_lists[0]),
["is_file", ..] => filetype_is_file::check(cx, expr, arg_lists[0]),
["map", "as_ref"] => {
option_as_ref_deref::check(cx, expr, arg_lists[1], arg_lists[0], false, self.msrv.as_ref())
},
Expand Down Expand Up @@ -3859,38 +3860,6 @@ fn check_pointer_offset(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir:
}
}

fn lint_filetype_is_file(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
let ty = cx.typeck_results().expr_ty(&args[0]);

if !match_type(cx, ty, &paths::FILE_TYPE) {
return;
}

let span: Span;
let verb: &str;
let lint_unary: &str;
let help_unary: &str;
if_chain! {
if let Some(parent) = get_parent_expr(cx, expr);
if let hir::ExprKind::Unary(op, _) = parent.kind;
if op == hir::UnOp::Not;
then {
lint_unary = "!";
verb = "denies";
help_unary = "";
span = parent.span;
} else {
lint_unary = "";
verb = "covers";
help_unary = "!";
span = expr.span;
}
}
let lint_msg = format!("`{}FileType::is_file()` only {} regular files", lint_unary, verb);
let help_msg = format!("use `{}FileType::is_dir()` instead", help_unary);
span_lint_and_help(cx, FILETYPE_IS_FILE, span, &lint_msg, None, &help_msg);
}

fn lint_from_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
let ty = cx.typeck_results().expr_ty(expr);
let arg_ty = cx.typeck_results().expr_ty(&args[0]);
Expand Down

0 comments on commit 8623b33

Please sign in to comment.