]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/filetype_is_file.rs
Rollup merge of #105829 - the8472:tidy-style, r=jyn514
[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::get_parent_expr;
3 use clippy_utils::ty::is_type_diagnostic_item;
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 use rustc_span::sym;
9
10 use super::FILETYPE_IS_FILE;
11
12 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
13     let ty = cx.typeck_results().expr_ty(recv);
14
15     if !is_type_diagnostic_item(cx, ty, sym::FileType) {
16         return;
17     }
18
19     let span: Span;
20     let verb: &str;
21     let lint_unary: &str;
22     let help_unary: &str;
23     if_chain! {
24         if let Some(parent) = get_parent_expr(cx, expr);
25         if let hir::ExprKind::Unary(op, _) = parent.kind;
26         if op == hir::UnOp::Not;
27         then {
28             lint_unary = "!";
29             verb = "denies";
30             help_unary = "";
31             span = parent.span;
32         } else {
33             lint_unary = "";
34             verb = "covers";
35             help_unary = "!";
36             span = expr.span;
37         }
38     }
39     let lint_msg = format!("`{lint_unary}FileType::is_file()` only {verb} regular files");
40     let help_msg = format!("use `{help_unary}FileType::is_dir()` instead");
41     span_lint_and_help(cx, FILETYPE_IS_FILE, span, &lint_msg, None, &help_msg);
42 }