1
1
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;
4
3
5
- use rustc_ast:: ast ;
4
+ use rustc_ast:: { Expr , ExprKind } ;
6
5
use rustc_errors:: Applicability ;
7
- use rustc_hir:: { Expr , ExprKind } ;
8
- use rustc_lint:: { LateContext , LateLintPass } ;
6
+ use rustc_lint:: { EarlyContext , EarlyLintPass } ;
9
7
use rustc_session:: declare_lint_pass;
10
8
11
9
declare_clippy_lint ! {
@@ -40,19 +38,11 @@ declare_clippy_lint! {
40
38
41
39
declare_lint_pass ! ( NeedlessParensOnRangeLiterals => [ NEEDLESS_PARENS_ON_RANGE_LITERALS ] ) ;
42
40
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
48
44
// 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 ( ) )
56
46
{
57
47
let mut applicability = Applicability :: MachineApplicable ;
58
48
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) {
68
58
}
69
59
}
70
60
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 {
74
64
if let Some ( start) = start {
75
65
check_for_parens ( cx, start, true ) ;
76
66
}
0 commit comments