Skip to content

Commit ef2018c

Browse files
committed
Auto merge of #10043 - phtown:master, r=Jarcho
Add lint `almost_complete_digit_range` changelog: [`almost_complete_digit_range`]: Add digit analog to `almost_complete_letter_range` I have added a lint that will detect `'0'..'9'` and suggest converting it to `'0'..='9'`, the same way `almost_complete_letter_range` does for ascii letters. I tied into the implementation of `AlmostCompleteLetterRange` in order to do that, but I didn't change its interface. This is my first contribution to Clippy, so please let me know if there's anything I should do differently. I'll be happy to incorporate any suggestions you have. Thanks!
2 parents 3492856 + 4c80f21 commit ef2018c

15 files changed

+390
-192
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,6 +3875,7 @@ Released 2018-09-13
38753875
[`alloc_instead_of_core`]: https://rust-lang.github.io/rust-clippy/master/index.html#alloc_instead_of_core
38763876
[`allow_attributes_without_reason`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes_without_reason
38773877
[`almost_complete_letter_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_letter_range
3878+
[`almost_complete_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_range
38783879
[`almost_swapped`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped
38793880
[`approx_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
38803881
[`arithmetic_side_effects`]: https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects

clippy_lints/src/almost_complete_letter_range.rs renamed to clippy_lints/src/almost_complete_range.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_span::Span;
1010

1111
declare_clippy_lint! {
1212
/// ### What it does
13-
/// Checks for ranges which almost include the entire range of letters from 'a' to 'z', but
14-
/// don't because they're a half open range.
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.
1515
///
1616
/// ### Why is this bad?
1717
/// This (`'a'..'z'`) is almost certainly a typo meant to include all letters.
@@ -25,21 +25,21 @@ declare_clippy_lint! {
2525
/// let _ = 'a'..='z';
2626
/// ```
2727
#[clippy::version = "1.63.0"]
28-
pub ALMOST_COMPLETE_LETTER_RANGE,
28+
pub ALMOST_COMPLETE_RANGE,
2929
suspicious,
30-
"almost complete letter range"
30+
"almost complete range"
3131
}
32-
impl_lint_pass!(AlmostCompleteLetterRange => [ALMOST_COMPLETE_LETTER_RANGE]);
32+
impl_lint_pass!(AlmostCompleteRange => [ALMOST_COMPLETE_RANGE]);
3333

34-
pub struct AlmostCompleteLetterRange {
34+
pub struct AlmostCompleteRange {
3535
msrv: Msrv,
3636
}
37-
impl AlmostCompleteLetterRange {
37+
impl AlmostCompleteRange {
3838
pub fn new(msrv: Msrv) -> Self {
3939
Self { msrv }
4040
}
4141
}
42-
impl EarlyLintPass for AlmostCompleteLetterRange {
42+
impl EarlyLintPass for AlmostCompleteRange {
4343
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
4444
if let ExprKind::Range(Some(start), Some(end), RangeLimits::HalfOpen) = &e.kind {
4545
let ctxt = e.span.ctxt();
@@ -87,14 +87,18 @@ fn check_range(cx: &EarlyContext<'_>, span: Span, start: &Expr, end: &Expr, sugg
8787
Ok(LitKind::Byte(b'A') | LitKind::Char('A')),
8888
Ok(LitKind::Byte(b'Z') | LitKind::Char('Z')),
8989
)
90+
| (
91+
Ok(LitKind::Byte(b'0') | LitKind::Char('0')),
92+
Ok(LitKind::Byte(b'9') | LitKind::Char('9')),
93+
)
9094
)
9195
&& !in_external_macro(cx.sess(), span)
9296
{
9397
span_lint_and_then(
9498
cx,
95-
ALMOST_COMPLETE_LETTER_RANGE,
99+
ALMOST_COMPLETE_RANGE,
96100
span,
97-
"almost complete ascii letter range",
101+
"almost complete ascii range",
98102
|diag| {
99103
if let Some((span, sugg)) = sugg {
100104
diag.span_suggestion(

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
3535
crate::utils::internal_lints::produce_ice::PRODUCE_ICE_INFO,
3636
#[cfg(feature = "internal")]
3737
crate::utils::internal_lints::unnecessary_def_path::UNNECESSARY_DEF_PATH_INFO,
38-
crate::almost_complete_letter_range::ALMOST_COMPLETE_LETTER_RANGE_INFO,
38+
crate::almost_complete_range::ALMOST_COMPLETE_RANGE_INFO,
3939
crate::approx_const::APPROX_CONSTANT_INFO,
4040
crate::as_conversions::AS_CONVERSIONS_INFO,
4141
crate::asm_syntax::INLINE_ASM_X86_ATT_SYNTAX_INFO,

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ mod declared_lints;
6666
mod renamed_lints;
6767

6868
// begin lints modules, do not remove this comment, it’s used in `update_lints`
69-
mod almost_complete_letter_range;
69+
mod almost_complete_range;
7070
mod approx_const;
7171
mod as_conversions;
7272
mod asm_syntax;
@@ -876,7 +876,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
876876
store.register_late_pass(|_| Box::new(rc_clone_in_vec_init::RcCloneInVecInit));
877877
store.register_early_pass(|| Box::<duplicate_mod::DuplicateMod>::default());
878878
store.register_early_pass(|| Box::new(unused_rounding::UnusedRounding));
879-
store.register_early_pass(move || Box::new(almost_complete_letter_range::AlmostCompleteLetterRange::new(msrv())));
879+
store.register_early_pass(move || Box::new(almost_complete_range::AlmostCompleteRange::new(msrv())));
880880
store.register_late_pass(|_| Box::new(swap_ptr_to_ref::SwapPtrToRef));
881881
store.register_late_pass(|_| Box::new(mismatching_type_param_order::TypeParamMismatch));
882882
store.register_late_pass(|_| Box::new(read_zero_byte_vec::ReadZeroByteVec));

clippy_lints/src/renamed_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#[rustfmt::skip]
44
pub static RENAMED_LINTS: &[(&str, &str)] = &[
5+
("clippy::almost_complete_letter_range", "clippy::almost_complete_range"),
56
("clippy::blacklisted_name", "clippy::disallowed_names"),
67
("clippy::block_in_if_condition_expr", "clippy::blocks_in_if_conditions"),
78
("clippy::block_in_if_condition_stmt", "clippy::blocks_in_if_conditions"),

tests/ui/almost_complete_letter_range.stderr

Lines changed: 0 additions & 113 deletions
This file was deleted.

tests/ui/almost_complete_letter_range.fixed renamed to tests/ui/almost_complete_range.fixed

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
#![feature(exclusive_range_pattern)]
66
#![feature(stmt_expr_attributes)]
7-
#![warn(clippy::almost_complete_letter_range)]
7+
#![warn(clippy::almost_complete_range)]
88
#![allow(ellipsis_inclusive_range_patterns)]
99
#![allow(clippy::needless_parens_on_range_literals)]
10+
#![allow(clippy::double_parens)]
1011

1112
#[macro_use]
1213
extern crate macro_rules;
@@ -16,10 +17,22 @@ macro_rules! a {
1617
'a'
1718
};
1819
}
20+
macro_rules! A {
21+
() => {
22+
'A'
23+
};
24+
}
25+
macro_rules! zero {
26+
() => {
27+
'0'
28+
};
29+
}
1930

2031
macro_rules! b {
2132
() => {
2233
let _ = 'a'..='z';
34+
let _ = 'A'..='Z';
35+
let _ = '0'..='9';
2336
};
2437
}
2538

@@ -28,52 +41,68 @@ fn main() {
2841
{
2942
let _ = ('a') ..='z';
3043
let _ = 'A' ..= ('Z');
44+
let _ = ((('0'))) ..= ('9');
3145
}
3246

3347
let _ = 'b'..'z';
3448
let _ = 'B'..'Z';
49+
let _ = '1'..'9';
3550

3651
let _ = (b'a')..=(b'z');
3752
let _ = b'A'..=b'Z';
53+
let _ = b'0'..=b'9';
3854

3955
let _ = b'b'..b'z';
4056
let _ = b'B'..b'Z';
57+
let _ = b'1'..b'9';
4158

4259
let _ = a!()..='z';
60+
let _ = A!()..='Z';
61+
let _ = zero!()..='9';
4362

4463
let _ = match 0u8 {
4564
b'a'..=b'z' if true => 1,
4665
b'A'..=b'Z' if true => 2,
47-
b'b'..b'z' => 3,
48-
b'B'..b'Z' => 4,
49-
_ => 5,
66+
b'0'..=b'9' if true => 3,
67+
b'b'..b'z' => 4,
68+
b'B'..b'Z' => 5,
69+
b'1'..b'9' => 6,
70+
_ => 7,
5071
};
5172

5273
let _ = match 'x' {
5374
'a'..='z' if true => 1,
5475
'A'..='Z' if true => 2,
55-
'b'..'z' => 3,
56-
'B'..'Z' => 4,
57-
_ => 5,
76+
'0'..='9' if true => 3,
77+
'b'..'z' => 4,
78+
'B'..'Z' => 5,
79+
'1'..'9' => 6,
80+
_ => 7,
5881
};
5982

60-
almost_complete_letter_range!();
83+
almost_complete_range!();
6184
b!();
6285
}
6386

6487
#[clippy::msrv = "1.25"]
6588
fn _under_msrv() {
6689
let _ = match 'a' {
6790
'a'...'z' => 1,
68-
_ => 2,
91+
'A'...'Z' => 2,
92+
'0'...'9' => 3,
93+
_ => 4,
6994
};
7095
}
7196

7297
#[clippy::msrv = "1.26"]
7398
fn _meets_msrv() {
7499
let _ = 'a'..='z';
100+
let _ = 'A'..='Z';
101+
let _ = '0'..='9';
75102
let _ = match 'a' {
76103
'a'..='z' => 1,
77-
_ => 2,
104+
'A'..='Z' => 1,
105+
'0'..='9' => 3,
106+
_ => 4,
78107
};
79108
}

0 commit comments

Comments
 (0)