Skip to content

Add lint almost_complete_digit_range #10043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3875,6 +3875,7 @@ Released 2018-09-13
[`alloc_instead_of_core`]: https://rust-lang.github.io/rust-clippy/master/index.html#alloc_instead_of_core
[`allow_attributes_without_reason`]: https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes_without_reason
[`almost_complete_letter_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_letter_range
[`almost_complete_range`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_complete_range
[`almost_swapped`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped
[`approx_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
[`arithmetic_side_effects`]: https://rust-lang.github.io/rust-clippy/master/index.html#arithmetic_side_effects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use rustc_span::Span;

declare_clippy_lint! {
/// ### What it does
/// Checks for ranges which almost include the entire range of letters from 'a' to 'z', but
/// don't because they're a half open range.
/// Checks for ranges which almost include the entire range of letters from 'a' to 'z'
/// or digits from '0' to '9', but don't because they're a half open range.
///
/// ### Why is this bad?
/// This (`'a'..'z'`) is almost certainly a typo meant to include all letters.
Expand All @@ -25,21 +25,21 @@ declare_clippy_lint! {
/// let _ = 'a'..='z';
/// ```
#[clippy::version = "1.63.0"]
pub ALMOST_COMPLETE_LETTER_RANGE,
pub ALMOST_COMPLETE_RANGE,
suspicious,
"almost complete letter range"
"almost complete range"
}
impl_lint_pass!(AlmostCompleteLetterRange => [ALMOST_COMPLETE_LETTER_RANGE]);
impl_lint_pass!(AlmostCompleteRange => [ALMOST_COMPLETE_RANGE]);

pub struct AlmostCompleteLetterRange {
pub struct AlmostCompleteRange {
msrv: Msrv,
}
impl AlmostCompleteLetterRange {
impl AlmostCompleteRange {
pub fn new(msrv: Msrv) -> Self {
Self { msrv }
}
}
impl EarlyLintPass for AlmostCompleteLetterRange {
impl EarlyLintPass for AlmostCompleteRange {
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
if let ExprKind::Range(Some(start), Some(end), RangeLimits::HalfOpen) = &e.kind {
let ctxt = e.span.ctxt();
Expand Down Expand Up @@ -87,14 +87,18 @@ fn check_range(cx: &EarlyContext<'_>, span: Span, start: &Expr, end: &Expr, sugg
Ok(LitKind::Byte(b'A') | LitKind::Char('A')),
Ok(LitKind::Byte(b'Z') | LitKind::Char('Z')),
)
| (
Ok(LitKind::Byte(b'0') | LitKind::Char('0')),
Ok(LitKind::Byte(b'9') | LitKind::Char('9')),
)
)
&& !in_external_macro(cx.sess(), span)
{
span_lint_and_then(
cx,
ALMOST_COMPLETE_LETTER_RANGE,
ALMOST_COMPLETE_RANGE,
span,
"almost complete ascii letter range",
"almost complete ascii range",
|diag| {
if let Some((span, sugg)) = sugg {
diag.span_suggestion(
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::utils::internal_lints::produce_ice::PRODUCE_ICE_INFO,
#[cfg(feature = "internal")]
crate::utils::internal_lints::unnecessary_def_path::UNNECESSARY_DEF_PATH_INFO,
crate::almost_complete_letter_range::ALMOST_COMPLETE_LETTER_RANGE_INFO,
crate::almost_complete_range::ALMOST_COMPLETE_RANGE_INFO,
crate::approx_const::APPROX_CONSTANT_INFO,
crate::as_conversions::AS_CONVERSIONS_INFO,
crate::asm_syntax::INLINE_ASM_X86_ATT_SYNTAX_INFO,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod declared_lints;
mod renamed_lints;

// begin lints modules, do not remove this comment, it’s used in `update_lints`
mod almost_complete_letter_range;
mod almost_complete_range;
mod approx_const;
mod as_conversions;
mod asm_syntax;
Expand Down Expand Up @@ -876,7 +876,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|_| Box::new(rc_clone_in_vec_init::RcCloneInVecInit));
store.register_early_pass(|| Box::<duplicate_mod::DuplicateMod>::default());
store.register_early_pass(|| Box::new(unused_rounding::UnusedRounding));
store.register_early_pass(move || Box::new(almost_complete_letter_range::AlmostCompleteLetterRange::new(msrv())));
store.register_early_pass(move || Box::new(almost_complete_range::AlmostCompleteRange::new(msrv())));
store.register_late_pass(|_| Box::new(swap_ptr_to_ref::SwapPtrToRef));
store.register_late_pass(|_| Box::new(mismatching_type_param_order::TypeParamMismatch));
store.register_late_pass(|_| Box::new(read_zero_byte_vec::ReadZeroByteVec));
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/renamed_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#[rustfmt::skip]
pub static RENAMED_LINTS: &[(&str, &str)] = &[
("clippy::almost_complete_letter_range", "clippy::almost_complete_range"),
("clippy::blacklisted_name", "clippy::disallowed_names"),
("clippy::block_in_if_condition_expr", "clippy::blocks_in_if_conditions"),
("clippy::block_in_if_condition_stmt", "clippy::blocks_in_if_conditions"),
Expand Down
113 changes: 0 additions & 113 deletions tests/ui/almost_complete_letter_range.stderr

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

#![feature(exclusive_range_pattern)]
#![feature(stmt_expr_attributes)]
#![warn(clippy::almost_complete_letter_range)]
#![warn(clippy::almost_complete_range)]
#![allow(ellipsis_inclusive_range_patterns)]
#![allow(clippy::needless_parens_on_range_literals)]
#![allow(clippy::double_parens)]

#[macro_use]
extern crate macro_rules;
Expand All @@ -16,10 +17,22 @@ macro_rules! a {
'a'
};
}
macro_rules! A {
() => {
'A'
};
}
macro_rules! zero {
() => {
'0'
};
}

macro_rules! b {
() => {
let _ = 'a'..='z';
let _ = 'A'..='Z';
let _ = '0'..='9';
};
}

Expand All @@ -28,52 +41,68 @@ fn main() {
{
let _ = ('a') ..='z';
let _ = 'A' ..= ('Z');
let _ = ((('0'))) ..= ('9');
}

let _ = 'b'..'z';
let _ = 'B'..'Z';
let _ = '1'..'9';

let _ = (b'a')..=(b'z');
let _ = b'A'..=b'Z';
let _ = b'0'..=b'9';

let _ = b'b'..b'z';
let _ = b'B'..b'Z';
let _ = b'1'..b'9';

let _ = a!()..='z';
let _ = A!()..='Z';
let _ = zero!()..='9';

let _ = match 0u8 {
b'a'..=b'z' if true => 1,
b'A'..=b'Z' if true => 2,
b'b'..b'z' => 3,
b'B'..b'Z' => 4,
_ => 5,
b'0'..=b'9' if true => 3,
b'b'..b'z' => 4,
b'B'..b'Z' => 5,
b'1'..b'9' => 6,
_ => 7,
};

let _ = match 'x' {
'a'..='z' if true => 1,
'A'..='Z' if true => 2,
'b'..'z' => 3,
'B'..'Z' => 4,
_ => 5,
'0'..='9' if true => 3,
'b'..'z' => 4,
'B'..'Z' => 5,
'1'..'9' => 6,
_ => 7,
};

almost_complete_letter_range!();
almost_complete_range!();
b!();
}

#[clippy::msrv = "1.25"]
fn _under_msrv() {
let _ = match 'a' {
'a'...'z' => 1,
_ => 2,
'A'...'Z' => 2,
'0'...'9' => 3,
_ => 4,
};
}

#[clippy::msrv = "1.26"]
fn _meets_msrv() {
let _ = 'a'..='z';
let _ = 'A'..='Z';
let _ = '0'..='9';
let _ = match 'a' {
'a'..='z' => 1,
_ => 2,
'A'..='Z' => 1,
'0'..='9' => 3,
_ => 4,
};
}
Loading