]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/to_digit_is_some.rs
New upstream version 1.66.0+dfsg1
[rustc.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, to_digit_expr, [], _) = &expr.kind;
43 if is_some_path.ident.name.as_str() == "is_some";
44 then {
45 let match_result = match &to_digit_expr.kind {
46 hir::ExprKind::MethodCall(to_digits_path, char_arg, [radix_arg], _) => {
47 if_chain! {
48 if to_digits_path.ident.name.as_str() == "to_digit";
49 let char_arg_ty = cx.typeck_results().expr_ty_adjusted(char_arg);
50 if *char_arg_ty.kind() == ty::Char;
51 then {
52 Some((true, *char_arg, radix_arg))
53 } else {
54 None
55 }
56 }
57 }
58 hir::ExprKind::Call(to_digits_call, to_digit_args) => {
59 if_chain! {
60 if let [char_arg, radix_arg] = *to_digit_args;
61 if let hir::ExprKind::Path(to_digits_path) = &to_digits_call.kind;
62 if let to_digits_call_res = cx.qpath_res(to_digits_path, to_digits_call.hir_id);
63 if let Some(to_digits_def_id) = to_digits_call_res.opt_def_id();
64 if match_def_path(cx, to_digits_def_id, &["core", "char", "methods", "<impl char>", "to_digit"]);
65 then {
66 Some((false, char_arg, radix_arg))
67 } else {
68 None
69 }
70 }
71 }
72 _ => None
73 };
74
75 if let Some((is_method_call, char_arg, radix_arg)) = match_result {
76 let mut applicability = Applicability::MachineApplicable;
77 let char_arg_snip = snippet_with_applicability(cx, char_arg.span, "_", &mut applicability);
78 let radix_snip = snippet_with_applicability(cx, radix_arg.span, "_", &mut applicability);
79
80 span_lint_and_sugg(
81 cx,
82 TO_DIGIT_IS_SOME,
83 expr.span,
84 "use of `.to_digit(..).is_some()`",
85 "try this",
86 if is_method_call {
87 format!("{char_arg_snip}.is_digit({radix_snip})")
88 } else {
89 format!("char::is_digit({char_arg_snip}, {radix_snip})")
90 },
91 applicability,
92 );
93 }
94 }
95 }
96 }
97 }