Rewrite check to account for floating point literals

This commit is contained in:
Dennis Luxen 2022-06-04 21:44:37 +02:00
parent 5948959c7e
commit 90c8463d7e
5 changed files with 63 additions and 24 deletions

View file

@ -747,7 +747,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_early_pass(|| Box::new(collapsible_if::CollapsibleIf)); store.register_early_pass(|| Box::new(collapsible_if::CollapsibleIf));
store.register_early_pass(|| Box::new(items_after_statements::ItemsAfterStatements)); store.register_early_pass(|| Box::new(items_after_statements::ItemsAfterStatements));
store.register_early_pass(|| Box::new(precedence::Precedence)); store.register_early_pass(|| Box::new(precedence::Precedence));
store.register_early_pass(|| Box::new(needless_parens_on_range_literal::NeedlessParensOnRangeLiteral)); store.register_late_pass(|| Box::new(needless_parens_on_range_literal::NeedlessParensOnRangeLiteral));
store.register_early_pass(|| Box::new(needless_continue::NeedlessContinue)); store.register_early_pass(|| Box::new(needless_continue::NeedlessContinue));
store.register_early_pass(|| Box::new(redundant_else::RedundantElse)); store.register_early_pass(|| Box::new(redundant_else::RedundantElse));
store.register_late_pass(|| Box::new(create_dir::CreateDir)); store.register_late_pass(|| Box::new(create_dir::CreateDir));

View file

@ -17,10 +17,17 @@
//! ``` //! ```
//! //!
use clippy_utils::{diagnostics::span_lint_and_then, source::snippet_opt}; use clippy_utils::{
use rustc_ast::ast::{Expr, ExprKind}; diagnostics::span_lint_and_then,
higher,
source::{snippet, snippet_opt},
};
use rustc_ast::ast;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass}; use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! { declare_clippy_lint! {
@ -40,27 +47,47 @@ declare_clippy_lint! {
declare_lint_pass!(NeedlessParensOnRangeLiteral => [NEEDLESS_PARENS_ON_RANGE_LITERAL]); declare_lint_pass!(NeedlessParensOnRangeLiteral => [NEEDLESS_PARENS_ON_RANGE_LITERAL]);
fn check_for_parens(cx: &EarlyContext<'_>, e: &Expr) { fn snippet_enclosed_in_parenthesis(snippet: &str) -> bool {
if_chain! { snippet.starts_with('(') && snippet.ends_with(')')
if let ExprKind::Paren(ref start_statement) = &e.kind;
if let ExprKind::Lit(ref literal) = start_statement.kind;
then {
span_lint_and_then(cx, NEEDLESS_PARENS_ON_RANGE_LITERAL, e.span,
"needless parenthesis on range literal can be removed",
|diag| {
if let Some(suggestion) = snippet_opt(cx, literal.span) {
diag.span_suggestion(e.span, "try", suggestion, Applicability::MachineApplicable);
}
});
}
}
} }
impl EarlyLintPass for NeedlessParensOnRangeLiteral { fn check_for_parens(cx: &LateContext<'_>, e: &Expr<'_>, is_start: bool) {
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) { if is_start &&
if let ExprKind::Range(Some(start), Some(end), _) = &e.kind { let ExprKind::Lit(ref literal) = e.kind &&
check_for_parens(cx, start); let ast::LitKind::Float(_sym, ast::LitFloatType::Unsuffixed) = literal.node
check_for_parens(cx, end); {
// 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 paranthese surround the literal is that 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 {
span_lint_and_then(cx, NEEDLESS_PARENS_ON_RANGE_LITERAL, e.span,
"needless parenthesis on range literal can be removed",
|diag| {
if let Some(suggestion) = snippet_opt(cx, literal.span) {
diag.span_suggestion(e.span, "try", suggestion, Applicability::MachineApplicable);
}
});
}
}
}
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);
} }
} }
} }

View file

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

View file

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

View file

@ -12,5 +12,11 @@ error: needless parenthesis on range literal can be removed
LL | let _ = ('a')..=('z'); LL | let _ = ('a')..=('z');
| ^^^^^ help: try: `'z'` | ^^^^^ help: try: `'z'`
error: aborting due to 2 previous errors 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