Skip to content

Commit f302c75

Browse files
committed
make early-pass
1 parent c42f540 commit f302c75

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
617617
store.register_late_pass(move |_| Box::new(collapsible_if::CollapsibleIf::new(conf)));
618618
store.register_late_pass(|_| Box::new(items_after_statements::ItemsAfterStatements));
619619
store.register_early_pass(|| Box::new(precedence::Precedence));
620-
store.register_late_pass(|_| Box::new(needless_parens_on_range_literals::NeedlessParensOnRangeLiterals));
620+
store.register_early_pass(|| Box::new(needless_parens_on_range_literals::NeedlessParensOnRangeLiterals));
621621
store.register_early_pass(|| Box::new(needless_continue::NeedlessContinue));
622622
store.register_early_pass(|| Box::new(redundant_else::RedundantElse));
623623
store.register_late_pass(|_| Box::new(create_dir::CreateDir));

clippy_lints/src/needless_parens_on_range_literals.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::higher;
3-
use clippy_utils::source::{SpanRangeExt, snippet_with_applicability};
2+
use clippy_utils::source::snippet_with_applicability;
43

5-
use rustc_ast::ast;
4+
use rustc_ast::{Expr, ExprKind};
65
use rustc_errors::Applicability;
7-
use rustc_hir::{Expr, ExprKind};
8-
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_lint::{EarlyContext, EarlyLintPass};
97
use rustc_session::declare_lint_pass;
108

119
declare_clippy_lint! {
@@ -40,19 +38,11 @@ declare_clippy_lint! {
4038

4139
declare_lint_pass!(NeedlessParensOnRangeLiterals => [NEEDLESS_PARENS_ON_RANGE_LITERALS]);
4240

43-
fn check_for_parens(cx: &LateContext<'_>, e: &Expr<'_>, is_start: bool) {
44-
if is_start
45-
&& let ExprKind::Lit(literal) = e.kind
46-
&& let ast::LitKind::Float(_sym, ast::LitFloatType::Unsuffixed) = literal.node
47-
{
41+
fn check_for_parens(cx: &EarlyContext<'_>, e: &Expr, is_start: bool) {
42+
if let ExprKind::Paren(literal) = &e.kind
43+
&& let ExprKind::Lit(lit) = &literal.kind
4844
// don't check floating point literals on the start expression of a range
49-
return;
50-
}
51-
if let ExprKind::Lit(literal) = e.kind
52-
// the indicator that parenthesis surround the literal is that the span of the expression and the literal differ
53-
&& literal.span != e.span
54-
// inspect the source code of the expression for parenthesis
55-
&& e.span.check_source_text(cx, |s| s.starts_with('(') && s.ends_with(')'))
45+
&& !(is_start && lit.kind == rustc_ast::token::LitKind::Float && lit.suffix.is_none())
5646
{
5747
let mut applicability = Applicability::MachineApplicable;
5848
let suggestion = snippet_with_applicability(cx, literal.span, "_", &mut applicability);
@@ -68,9 +58,9 @@ fn check_for_parens(cx: &LateContext<'_>, e: &Expr<'_>, is_start: bool) {
6858
}
6959
}
7060

71-
impl<'tcx> LateLintPass<'tcx> for NeedlessParensOnRangeLiterals {
72-
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
73-
if let Some(higher::Range { start, end, .. }) = higher::Range::hir(expr) {
61+
impl EarlyLintPass for NeedlessParensOnRangeLiterals {
62+
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
63+
if let ExprKind::Range(start, end, ..) = &expr.kind {
7464
if let Some(start) = start {
7565
check_for_parens(cx, start, true);
7666
}

0 commit comments

Comments
 (0)