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