Implement support for implicit start and end

This commit is contained in:
Dennis Luxen 2022-06-04 22:01:52 +02:00
parent 2e6903ccf9
commit 0409306bb0
4 changed files with 32 additions and 12 deletions

View File

@ -75,15 +75,13 @@ fn check_for_parens(cx: &LateContext<'_>, e: &Expr<'_>, is_start: bool) {
impl<'tcx> LateLintPass<'tcx> for NeedlessParensOnRangeLiteral {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
// if let higher::Range { start, end, limits } = &e.kind {
if let Some(higher::Range {
start: Some(start),
end: Some(end),
..
}) = higher::Range::hir(expr)
{
check_for_parens(cx, start, true);
check_for_parens(cx, end, false);
if let Some(higher::Range { start, end, .. }) = higher::Range::hir(expr) {
if let Some(start) = start {
check_for_parens(cx, start, true);
}
if let Some(end) = end {
check_for_parens(cx, end, false);
}
}
}
}

View File

@ -6,7 +6,9 @@
fn main() {
let _ = 'a'..='z';
let _ = 'a'..='z';
let _ = 'a'..'z';
let _ = (1.)..2.;
let _ = (1.)..2.;
let _ = 'a'..;
let _ = ..'z';
}

View File

@ -6,7 +6,9 @@
fn main() {
let _ = ('a')..=('z');
let _ = 'a'..='z';
let _ = 'a'..('z');
let _ = (1.)..2.;
let _ = (1.)..(2.);
let _ = ('a')..;
let _ = ..('z');
}

View File

@ -12,11 +12,29 @@ error: needless parenthesis on range literal can be removed
LL | let _ = ('a')..=('z');
| ^^^^^ help: try: `'z'`
error: needless parenthesis on range literal can be removed
--> $DIR/needless_parens_on_range_literal.rs:9:18
|
LL | let _ = 'a'..('z');
| ^^^^^ help: try: `'z'`
error: needless parenthesis on range literal can be removed
--> $DIR/needless_parens_on_range_literal.rs:11:19
|
LL | let _ = (1.)..(2.);
| ^^^^ help: try: `2.`
error: aborting due to 3 previous errors
error: needless parenthesis on range literal can be removed
--> $DIR/needless_parens_on_range_literal.rs:12:13
|
LL | let _ = ('a')..;
| ^^^^^ help: try: `'a'`
error: needless parenthesis on range literal can be removed
--> $DIR/needless_parens_on_range_literal.rs:13:15
|
LL | let _ = ..('z');
| ^^^^^ help: try: `'z'`
error: aborting due to 6 previous errors