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