]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/to_digit_is_some.rs
Rollup merge of #90741 - mbartlett21:patch-4, r=dtolnay
[rust.git] / src / tools / clippy / clippy_lints / src / to_digit_is_some.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::match_def_path;
3 use clippy_utils::source::snippet_with_applicability;
4 use if_chain::if_chain;
5 use rustc_errors::Applicability;
6 use rustc_hir as hir;
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_middle::ty;
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
10
11 declare_clippy_lint! {
12     /// ### What it does
13     /// Checks for `.to_digit(..).is_some()` on `char`s.
14     ///
15     /// ### Why is this bad?
16     /// This is a convoluted way of checking if a `char` is a digit. It's
17     /// more straight forward to use the dedicated `is_digit` method.
18     ///
19     /// ### Example
20     /// ```rust
21     /// # let c = 'c';
22     /// # let radix = 10;
23     /// let is_digit = c.to_digit(radix).is_some();
24     /// ```
25     /// can be written as:
26     /// ```
27     /// # let c = 'c';
28     /// # let radix = 10;
29     /// let is_digit = c.is_digit(radix);
30     /// ```
31     #[clippy::version = "1.41.0"]
32     pub TO_DIGIT_IS_SOME,
33     style,
34     "`char.is_digit()` is clearer"
35 }
36
37 declare_lint_pass!(ToDigitIsSome => [TO_DIGIT_IS_SOME]);
38
39 impl<'tcx> LateLintPass<'tcx> for ToDigitIsSome {
40     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
41         if_chain! {
42             if let hir::ExprKind::MethodCall(is_some_path, _, is_some_args, _) = &expr.kind;
43             if is_some_path.ident.name.as_str() == "is_some";
44             if let [to_digit_expr] = &**is_some_args;
45             then {
46                 let match_result = match &to_digit_expr.kind {
47                     hir::ExprKind::MethodCall(to_digits_path, _, to_digit_args, _) => {
48                         if_chain! {
49                             if let [char_arg, radix_arg] = &**to_digit_args;
50                             if to_digits_path.ident.name.as_str() == "to_digit";
51                             let char_arg_ty = cx.typeck_results().expr_ty_adjusted(char_arg);
52                             if *char_arg_ty.kind() == ty::Char;
53                             then {
54                                 Some((true, char_arg, radix_arg))
55                             } else {
56                                 None
57                             }
58                         }
59                     }
60                     hir::ExprKind::Call(to_digits_call, to_digit_args) => {
61                         if_chain! {
62                             if let [char_arg, radix_arg] = &**to_digit_args;
63                             if let hir::ExprKind::Path(to_digits_path) = &to_digits_call.kind;
64                             if let to_digits_call_res = cx.qpath_res(to_digits_path, to_digits_call.hir_id);
65                             if let Some(to_digits_def_id) = to_digits_call_res.opt_def_id();
66                             if match_def_path(cx, to_digits_def_id, &["core", "char", "methods", "<impl char>", "to_digit"]);
67                             then {
68                                 Some((false, char_arg, radix_arg))
69                             } else {
70                                 None
71                             }
72                         }
73                     }
74                     _ => None
75                 };
76
77                 if let Some((is_method_call, char_arg, radix_arg)) = match_result {
78                     let mut applicability = Applicability::MachineApplicable;
79                     let char_arg_snip = snippet_with_applicability(cx, char_arg.span, "_", &mut applicability);
80                     let radix_snip = snippet_with_applicability(cx, radix_arg.span, "_", &mut applicability);
81
82                     span_lint_and_sugg(
83                         cx,
84                         TO_DIGIT_IS_SOME,
85                         expr.span,
86                         "use of `.to_digit(..).is_some()`",
87                         "try this",
88                         if is_method_call {
89                             format!("{}.is_digit({})", char_arg_snip, radix_snip)
90                         } else {
91                             format!("char::is_digit({}, {})", char_arg_snip, radix_snip)
92                         },
93                         applicability,
94                     );
95                 }
96             }
97         }
98     }
99 }