]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/almost_complete_range.rs
Merge commit '4bdfb0741dbcecd5279a2635c3280726db0604b5' into clippyup
[rust.git] / src / tools / clippy / clippy_lints / src / almost_complete_range.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::msrvs::{self, Msrv};
3 use clippy_utils::source::{trim_span, walk_span_to_context};
4 use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits};
5 use rustc_errors::Applicability;
6 use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
7 use rustc_middle::lint::in_external_macro;
8 use rustc_session::{declare_tool_lint, impl_lint_pass};
9 use rustc_span::Span;
10
11 declare_clippy_lint! {
12     /// ### What it does
13     /// Checks for ranges which almost include the entire range of letters from 'a' to 'z'
14     /// or digits from '0' to '9', but don't because they're a half open range.
15     ///
16     /// ### Why is this bad?
17     /// This (`'a'..'z'`) is almost certainly a typo meant to include all letters.
18     ///
19     /// ### Example
20     /// ```rust
21     /// let _ = 'a'..'z';
22     /// ```
23     /// Use instead:
24     /// ```rust
25     /// let _ = 'a'..='z';
26     /// ```
27     #[clippy::version = "1.63.0"]
28     pub ALMOST_COMPLETE_RANGE,
29     suspicious,
30     "almost complete range"
31 }
32 impl_lint_pass!(AlmostCompleteRange => [ALMOST_COMPLETE_RANGE]);
33
34 pub struct AlmostCompleteRange {
35     msrv: Msrv,
36 }
37 impl AlmostCompleteRange {
38     pub fn new(msrv: Msrv) -> Self {
39         Self { msrv }
40     }
41 }
42 impl EarlyLintPass for AlmostCompleteRange {
43     fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
44         if let ExprKind::Range(Some(start), Some(end), RangeLimits::HalfOpen) = &e.kind {
45             let ctxt = e.span.ctxt();
46             let sugg = if let Some(start) = walk_span_to_context(start.span, ctxt)
47                 && let Some(end) = walk_span_to_context(end.span, ctxt)
48                 && self.msrv.meets(msrvs::RANGE_INCLUSIVE)
49             {
50                 Some((trim_span(cx.sess().source_map(), start.between(end)), "..="))
51             } else {
52                 None
53             };
54             check_range(cx, e.span, start, end, sugg);
55         }
56     }
57
58     fn check_pat(&mut self, cx: &EarlyContext<'_>, p: &Pat) {
59         if let PatKind::Range(Some(start), Some(end), kind) = &p.kind
60             && matches!(kind.node, RangeEnd::Excluded)
61         {
62             let sugg = if self.msrv.meets(msrvs::RANGE_INCLUSIVE) {
63                 "..="
64             } else {
65                 "..."
66             };
67             check_range(cx, p.span, start, end, Some((kind.span, sugg)));
68         }
69     }
70
71     extract_msrv_attr!(EarlyContext);
72 }
73
74 fn check_range(cx: &EarlyContext<'_>, span: Span, start: &Expr, end: &Expr, sugg: Option<(Span, &str)>) {
75     if let ExprKind::Lit(start_token_lit) = start.peel_parens().kind
76         && let ExprKind::Lit(end_token_lit) = end.peel_parens().kind
77         && matches!(
78             (
79                 LitKind::from_token_lit(start_token_lit),
80                 LitKind::from_token_lit(end_token_lit),
81             ),
82             (
83                 Ok(LitKind::Byte(b'a') | LitKind::Char('a')),
84                 Ok(LitKind::Byte(b'z') | LitKind::Char('z'))
85             )
86             | (
87                 Ok(LitKind::Byte(b'A') | LitKind::Char('A')),
88                 Ok(LitKind::Byte(b'Z') | LitKind::Char('Z')),
89             )
90             | (
91                 Ok(LitKind::Byte(b'0') | LitKind::Char('0')),
92                 Ok(LitKind::Byte(b'9') | LitKind::Char('9')),
93             )
94         )
95         && !in_external_macro(cx.sess(), span)
96     {
97         span_lint_and_then(
98             cx,
99             ALMOST_COMPLETE_RANGE,
100             span,
101             "almost complete ascii range",
102             |diag| {
103                 if let Some((span, sugg)) = sugg {
104                     diag.span_suggestion(
105                         span,
106                         "use an inclusive range",
107                         sugg,
108                         Applicability::MaybeIncorrect,
109                     );
110                 }
111             }
112         );
113     }
114 }