]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/filetype_is_file.rs
Auto merge of #99422 - Dylan-DPC:rollup-htjofm6, r=Dylan-DPC
[rust.git] / src / tools / clippy / clippy_lints / src / methods / filetype_is_file.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::match_type;
3 use clippy_utils::{get_parent_expr, paths};
4 use if_chain::if_chain;
5 use rustc_hir as hir;
6 use rustc_lint::LateContext;
7 use rustc_span::source_map::Span;
8
9 use super::FILETYPE_IS_FILE;
10
11 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
12     let ty = cx.typeck_results().expr_ty(recv);
13
14     if !match_type(cx, ty, &paths::FILE_TYPE) {
15         return;
16     }
17
18     let span: Span;
19     let verb: &str;
20     let lint_unary: &str;
21     let help_unary: &str;
22     if_chain! {
23         if let Some(parent) = get_parent_expr(cx, expr);
24         if let hir::ExprKind::Unary(op, _) = parent.kind;
25         if op == hir::UnOp::Not;
26         then {
27             lint_unary = "!";
28             verb = "denies";
29             help_unary = "";
30             span = parent.span;
31         } else {
32             lint_unary = "";
33             verb = "covers";
34             help_unary = "!";
35             span = expr.span;
36         }
37     }
38     let lint_msg = format!("`{}FileType::is_file()` only {} regular files", lint_unary, verb);
39     let help_msg = format!("use `{}FileType::is_dir()` instead", help_unary);
40     span_lint_and_help(cx, FILETYPE_IS_FILE, span, &lint_msg, None, &help_msg);
41 }