-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lint
almost_complete_letter_range
- Loading branch information
Showing
11 changed files
with
347 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::source::{trim_span, walk_span_to_context}; | ||
use clippy_utils::{meets_msrv, msrvs}; | ||
use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; | ||
use rustc_semver::RustcVersion; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
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. | ||
/// | ||
/// ### Why is this bad? | ||
/// This (`'a'..'z'`) is almost certainly a typo meant to include all letters. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// let _ = 'a'..'z'; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// let _ = 'a'..='z'; | ||
/// ``` | ||
#[clippy::version = "1.63.0"] | ||
pub ALMOST_COMPLETE_LETTER_RANGE, | ||
suspicious, | ||
"almost complete letter range" | ||
} | ||
impl_lint_pass!(AlmostCompleteLetterRange => [ALMOST_COMPLETE_LETTER_RANGE]); | ||
|
||
pub struct AlmostCompleteLetterRange { | ||
msrv: Option<RustcVersion>, | ||
} | ||
impl AlmostCompleteLetterRange { | ||
pub fn new(msrv: Option<RustcVersion>) -> Self { | ||
Self { msrv } | ||
} | ||
} | ||
impl EarlyLintPass for AlmostCompleteLetterRange { | ||
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(); | ||
let span = if let Some(start) = walk_span_to_context(start.span, ctxt) | ||
&& let Some(end) = walk_span_to_context(end.span, ctxt) | ||
&& meets_msrv(self.msrv, msrvs::RANGE_INCLUSIVE) | ||
{ | ||
Some(trim_span(cx.sess().source_map(), start.between(end))) | ||
} else { | ||
None | ||
}; | ||
check_range(cx, e.span, start, end, span, "..="); | ||
} | ||
} | ||
|
||
fn check_pat(&mut self, cx: &EarlyContext<'_>, p: &Pat) { | ||
if let PatKind::Range(Some(start), Some(end), kind) = &p.kind | ||
&& matches!(kind.node, RangeEnd::Excluded) | ||
{ | ||
let sugg = if meets_msrv(self.msrv, msrvs::RANGE_INCLUSIVE) { | ||
"..=" | ||
} else { | ||
"..." | ||
}; | ||
check_range(cx, p.span, start, end, Some(kind.span), sugg); | ||
} | ||
} | ||
|
||
extract_msrv_attr!(EarlyContext); | ||
} | ||
|
||
fn check_range(cx: &EarlyContext<'_>, span: Span, start: &Expr, end: &Expr, op_span: Option<Span>, sugg: &str) { | ||
if let ExprKind::Lit(start_lit) = &start.peel_parens().kind | ||
&& let ExprKind::Lit(end_lit) = &end.peel_parens().kind | ||
&& matches!( | ||
(&start_lit.kind, &end_lit.kind), | ||
(LitKind::Byte(b'a') | LitKind::Char('a'), LitKind::Byte(b'z') | LitKind::Char('z')) | ||
| (LitKind::Byte(b'A') | LitKind::Char('A'), LitKind::Byte(b'Z') | LitKind::Char('Z')) | ||
) | ||
{ | ||
span_lint_and_then( | ||
cx, | ||
ALMOST_COMPLETE_LETTER_RANGE, | ||
span, | ||
"almost complete ascii letter range", | ||
|diag| { | ||
if let Some(span) = op_span { | ||
diag.span_suggestion( | ||
span, | ||
"use an inclusive range", | ||
sugg.to_owned(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// run-rustfix | ||
|
||
#![feature(custom_inner_attributes)] | ||
#![feature(exclusive_range_pattern)] | ||
#![feature(stmt_expr_attributes)] | ||
#![warn(clippy::almost_complete_letter_range)] | ||
|
||
macro_rules! a { | ||
() => { | ||
'a' | ||
}; | ||
} | ||
|
||
fn main() { | ||
#[rustfmt::skip] | ||
{ | ||
let _ = ('a') ..='z'; | ||
let _ = 'A' ..= ('Z'); | ||
} | ||
|
||
let _ = 'b'..'z'; | ||
let _ = 'B'..'Z'; | ||
|
||
let _ = (b'a')..=(b'z'); | ||
let _ = b'A'..=b'Z'; | ||
|
||
let _ = b'b'..b'z'; | ||
let _ = b'B'..b'Z'; | ||
|
||
let _ = a!()..='z'; | ||
|
||
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, | ||
}; | ||
|
||
let _ = match 'x' { | ||
'a'..='z' if true => 1, | ||
'A'..='Z' if true => 2, | ||
'b'..'z' => 3, | ||
'B'..'Z' => 4, | ||
_ => 5, | ||
}; | ||
} | ||
|
||
fn _under_msrv() { | ||
#![clippy::msrv = "1.25"] | ||
let _ = match 'a' { | ||
'a'...'z' => 1, | ||
_ => 2, | ||
}; | ||
} | ||
|
||
fn _meets_msrv() { | ||
#![clippy::msrv = "1.26"] | ||
let _ = match 'a' { | ||
'a'..='z' => 1, | ||
_ => 2, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// run-rustfix | ||
|
||
#![feature(custom_inner_attributes)] | ||
#![feature(exclusive_range_pattern)] | ||
#![feature(stmt_expr_attributes)] | ||
#![warn(clippy::almost_complete_letter_range)] | ||
|
||
macro_rules! a { | ||
() => { | ||
'a' | ||
}; | ||
} | ||
|
||
fn main() { | ||
#[rustfmt::skip] | ||
{ | ||
let _ = ('a') ..'z'; | ||
let _ = 'A' .. ('Z'); | ||
} | ||
|
||
let _ = 'b'..'z'; | ||
let _ = 'B'..'Z'; | ||
|
||
let _ = (b'a')..(b'z'); | ||
let _ = b'A'..b'Z'; | ||
|
||
let _ = b'b'..b'z'; | ||
let _ = b'B'..b'Z'; | ||
|
||
let _ = a!()..'z'; | ||
|
||
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, | ||
}; | ||
|
||
let _ = match 'x' { | ||
'a'..'z' if true => 1, | ||
'A'..'Z' if true => 2, | ||
'b'..'z' => 3, | ||
'B'..'Z' => 4, | ||
_ => 5, | ||
}; | ||
} | ||
|
||
fn _under_msrv() { | ||
#![clippy::msrv = "1.25"] | ||
let _ = match 'a' { | ||
'a'..'z' => 1, | ||
_ => 2, | ||
}; | ||
} | ||
|
||
fn _meets_msrv() { | ||
#![clippy::msrv = "1.26"] | ||
let _ = match 'a' { | ||
'a'..'z' => 1, | ||
_ => 2, | ||
}; | ||
} |
Oops, something went wrong.