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     pub TO_DIGIT_IS_SOME,
32     style,
33     "`char.is_digit()` is clearer"
34 }
35 
36 declare_lint_pass!(ToDigitIsSome => [TO_DIGIT_IS_SOME]);
37 
38 impl<'tcx> LateLintPass<'tcx> for ToDigitIsSome {
check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>)39     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
40         if_chain! {
41             if let hir::ExprKind::MethodCall(is_some_path, _, is_some_args, _) = &expr.kind;
42             if is_some_path.ident.name.as_str() == "is_some";
43             if let [to_digit_expr] = &**is_some_args;
44             then {
45                 let match_result = match &to_digit_expr.kind {
46                     hir::ExprKind::MethodCall(to_digits_path, _, to_digit_args, _) => {
47                         if_chain! {
48                             if let [char_arg, radix_arg] = &**to_digit_args;
49                             if to_digits_path.ident.name.as_str() == "to_digit";
50                             let char_arg_ty = cx.typeck_results().expr_ty_adjusted(char_arg);
51                             if *char_arg_ty.kind() == ty::Char;
52                             then {
53                                 Some((true, char_arg, radix_arg))
54                             } else {
55                                 None
56                             }
57                         }
58                     }
59                     hir::ExprKind::Call(to_digits_call, to_digit_args) => {
60                         if_chain! {
61                             if let [char_arg, radix_arg] = &**to_digit_args;
62                             if let hir::ExprKind::Path(to_digits_path) = &to_digits_call.kind;
63                             if let to_digits_call_res = cx.qpath_res(to_digits_path, to_digits_call.hir_id);
64                             if let Some(to_digits_def_id) = to_digits_call_res.opt_def_id();
65                             if match_def_path(cx, to_digits_def_id, &["core", "char", "methods", "<impl char>", "to_digit"]);
66                             then {
67                                 Some((false, char_arg, radix_arg))
68                             } else {
69                                 None
70                             }
71                         }
72                     }
73                     _ => None
74                 };
75 
76                 if let Some((is_method_call, char_arg, radix_arg)) = match_result {
77                     let mut applicability = Applicability::MachineApplicable;
78                     let char_arg_snip = snippet_with_applicability(cx, char_arg.span, "_", &mut applicability);
79                     let radix_snip = snippet_with_applicability(cx, radix_arg.span, "_", &mut applicability);
80 
81                     span_lint_and_sugg(
82                         cx,
83                         TO_DIGIT_IS_SOME,
84                         expr.span,
85                         "use of `.to_digit(..).is_some()`",
86                         "try this",
87                         if is_method_call {
88                             format!("{}.is_digit({})", char_arg_snip, radix_snip)
89                         } else {
90                             format!("char::is_digit({}, {})", char_arg_snip, radix_snip)
91                         },
92                         applicability,
93                     );
94                 }
95             }
96         }
97     }
98 }
99