-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #8933 - DennisOSRM:needless_braces_range_literal, r=dswij
Add new lint [`needless_parens_on_range_literals`] changelog: Adds a new lint [`needless_parens_on_range_literals`] to warn on needless braces on literals in a range statement For example, the lint would catch ```log error: needless parenthesis on range literals can be removed --> $DIR/needless_parens_on_range_literals.rs:8:13 | LL | let _ = ('a')..=('z'); | ^^^^^ help: try: `'a'` | = note: `-D clippy::needless-parens-on-range-literals` implied by `-D warnings` ```
- Loading branch information
Showing
12 changed files
with
175 additions
and
12 deletions.
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
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,87 @@ | ||
use clippy_utils::{ | ||
diagnostics::span_lint_and_then, | ||
higher, | ||
source::{snippet, snippet_with_applicability}, | ||
}; | ||
|
||
use rustc_ast::ast; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
|
||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// The lint checks for parenthesis on literals in range statements that are | ||
/// superfluous. | ||
/// | ||
/// ### Why is this bad? | ||
/// Having superfluous parenthesis makes the code less readable | ||
/// overhead when reading. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// for i in (0)..10 { | ||
/// println!("{i}"); | ||
/// } | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// | ||
/// ```rust | ||
/// for i in 0..10 { | ||
/// println!("{i}"); | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.63.0"] | ||
pub NEEDLESS_PARENS_ON_RANGE_LITERALS, | ||
style, | ||
"needless parenthesis on range literals can be removed" | ||
} | ||
|
||
declare_lint_pass!(NeedlessParensOnRangeLiterals => [NEEDLESS_PARENS_ON_RANGE_LITERALS]); | ||
|
||
fn snippet_enclosed_in_parenthesis(snippet: &str) -> bool { | ||
snippet.starts_with('(') && snippet.ends_with(')') | ||
} | ||
|
||
fn check_for_parens(cx: &LateContext<'_>, e: &Expr<'_>, is_start: bool) { | ||
if is_start && | ||
let ExprKind::Lit(ref literal) = e.kind && | ||
let ast::LitKind::Float(_sym, ast::LitFloatType::Unsuffixed) = literal.node | ||
{ | ||
// don't check floating point literals on the start expression of a range | ||
return; | ||
} | ||
if_chain! { | ||
if let ExprKind::Lit(ref literal) = e.kind; | ||
// the indicator that parenthesis surround the literal is that the span of the expression and the literal differ | ||
if (literal.span.data().hi - literal.span.data().lo) != (e.span.data().hi - e.span.data().lo); | ||
// inspect the source code of the expression for parenthesis | ||
if snippet_enclosed_in_parenthesis(&snippet(cx, e.span, "")); | ||
then { | ||
let mut applicability = Applicability::MachineApplicable; | ||
span_lint_and_then(cx, NEEDLESS_PARENS_ON_RANGE_LITERALS, e.span, | ||
"needless parenthesis on range literals can be removed", | ||
|diag| { | ||
let suggestion = snippet_with_applicability(cx, literal.span, "_", &mut applicability); | ||
diag.span_suggestion(e.span, "try", suggestion, applicability); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
impl<'tcx> LateLintPass<'tcx> for NeedlessParensOnRangeLiterals { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { | ||
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); | ||
} | ||
} | ||
} | ||
} |
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,14 @@ | ||
// run-rustfix | ||
// edition:2018 | ||
|
||
#![warn(clippy::needless_parens_on_range_literals)] | ||
#![allow(clippy::almost_complete_letter_range)] | ||
|
||
fn main() { | ||
let _ = 'a'..='z'; | ||
let _ = 'a'..'z'; | ||
let _ = (1.)..2.; | ||
let _ = (1.)..2.; | ||
let _ = 'a'..; | ||
let _ = ..'z'; | ||
} |
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,14 @@ | ||
// run-rustfix | ||
// edition:2018 | ||
|
||
#![warn(clippy::needless_parens_on_range_literals)] | ||
#![allow(clippy::almost_complete_letter_range)] | ||
|
||
fn main() { | ||
let _ = ('a')..=('z'); | ||
let _ = 'a'..('z'); | ||
let _ = (1.)..2.; | ||
let _ = (1.)..(2.); | ||
let _ = ('a')..; | ||
let _ = ..('z'); | ||
} |
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,40 @@ | ||
error: needless parenthesis on range literals can be removed | ||
--> $DIR/needless_parens_on_range_literals.rs:8:13 | ||
| | ||
LL | let _ = ('a')..=('z'); | ||
| ^^^^^ help: try: `'a'` | ||
| | ||
= note: `-D clippy::needless-parens-on-range-literals` implied by `-D warnings` | ||
|
||
error: needless parenthesis on range literals can be removed | ||
--> $DIR/needless_parens_on_range_literals.rs:8:21 | ||
| | ||
LL | let _ = ('a')..=('z'); | ||
| ^^^^^ help: try: `'z'` | ||
|
||
error: needless parenthesis on range literals can be removed | ||
--> $DIR/needless_parens_on_range_literals.rs:9:18 | ||
| | ||
LL | let _ = 'a'..('z'); | ||
| ^^^^^ help: try: `'z'` | ||
|
||
error: needless parenthesis on range literals can be removed | ||
--> $DIR/needless_parens_on_range_literals.rs:11:19 | ||
| | ||
LL | let _ = (1.)..(2.); | ||
| ^^^^ help: try: `2.` | ||
|
||
error: needless parenthesis on range literals can be removed | ||
--> $DIR/needless_parens_on_range_literals.rs:12:13 | ||
| | ||
LL | let _ = ('a')..; | ||
| ^^^^^ help: try: `'a'` | ||
|
||
error: needless parenthesis on range literals can be removed | ||
--> $DIR/needless_parens_on_range_literals.rs:13:15 | ||
| | ||
LL | let _ = ..('z'); | ||
| ^^^^^ help: try: `'z'` | ||
|
||
error: aborting due to 6 previous errors | ||
|