Skip to content

Commit

Permalink
Rewrite check to account for floating point literals
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisOSRM committed Jun 4, 2022
1 parent c65d840 commit 17595c6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 22 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(items_after_statements::ItemsAfterStatements));
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(redundant_else::RedundantElse));
store.register_late_pass(|| Box::new(create_dir::CreateDir));
Expand Down
67 changes: 47 additions & 20 deletions clippy_lints/src/needless_parens_on_range_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@
//! ```
//!
use clippy_utils::{diagnostics::span_lint_and_then, source::snippet_opt};
use rustc_ast::ast::{Expr, ExprKind};
use clippy_utils::{
diagnostics::span_lint_and_then,
higher,
source::{snippet, snippet_opt},
};

use rustc_ast::ast;
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};

declare_clippy_lint! {
Expand All @@ -40,27 +47,47 @@ declare_clippy_lint! {

declare_lint_pass!(NeedlessParensOnRangeLiteral => [NEEDLESS_PARENS_ON_RANGE_LITERAL]);

fn check_for_parens(cx: &EarlyContext<'_>, e: &Expr) {
if_chain! {
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);
}
});
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 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 EarlyLintPass for NeedlessParensOnRangeLiteral {
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
if let ExprKind::Range(Some(start), Some(end), _) = &e.kind {
check_for_parens(cx, start);
check_for_parens(cx, end);
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);
}
}
}
3 changes: 3 additions & 0 deletions tests/ui/needless_parens_on_range_literal.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

fn main() {
let _ = 'a'..='z';
let _ = 'a'..='z';
let _ = (1.)..2.;
let _ = (1.)..2.;
}
3 changes: 3 additions & 0 deletions tests/ui/needless_parens_on_range_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@

fn main() {
let _ = ('a')..=('z');
let _ = 'a'..='z';
let _ = (1.)..2.;
let _ = (1.)..(2.);
}
8 changes: 7 additions & 1 deletion tests/ui/needless_parens_on_range_literal.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ error: needless parenthesis on range literal can be removed
LL | let _ = ('a')..=('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

0 comments on commit 17595c6

Please sign in to comment.