2
2
3
3
use clippy_utils:: diagnostics:: span_lint_and_sugg;
4
4
use clippy_utils:: source:: snippet_opt;
5
- use rustc_ast:: ast:: { BinOpKind , Expr , ExprKind , Lit , LitKind } ;
5
+ use rustc_ast:: ast:: { BinOpKind , Expr , ExprKind , LitKind } ;
6
+ use rustc_ast:: token;
6
7
use rustc_errors:: Applicability ;
7
8
use rustc_lint:: { EarlyContext , EarlyLintPass } ;
8
9
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -52,8 +53,8 @@ enum Side {
52
53
53
54
impl IntPlusOne {
54
55
#[ expect( clippy:: cast_sign_loss) ]
55
- fn check_lit ( lit : & Lit , target_value : i128 ) -> bool {
56
- if let LitKind :: Int ( value, ..) = lit . kind {
56
+ fn check_lit ( token_lit : token :: Lit , target_value : i128 ) -> bool {
57
+ if let Ok ( LitKind :: Int ( value, ..) ) = LitKind :: from_token_lit ( token_lit ) {
57
58
return value == ( target_value as u128 ) ;
58
59
}
59
60
false
@@ -65,11 +66,11 @@ impl IntPlusOne {
65
66
( BinOpKind :: Ge , & ExprKind :: Binary ( ref lhskind, ref lhslhs, ref lhsrhs) , _) => {
66
67
match ( lhskind. node , & lhslhs. kind , & lhsrhs. kind ) {
67
68
// `-1 + x`
68
- ( BinOpKind :: Add , & ExprKind :: Lit ( ref lit) , _) if Self :: check_lit ( lit, -1 ) => {
69
+ ( BinOpKind :: Add , & ExprKind :: Lit ( lit) , _) if Self :: check_lit ( lit, -1 ) => {
69
70
Self :: generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: Lhs )
70
71
} ,
71
72
// `x - 1`
72
- ( BinOpKind :: Sub , _, & ExprKind :: Lit ( ref lit) ) if Self :: check_lit ( lit, 1 ) => {
73
+ ( BinOpKind :: Sub , _, & ExprKind :: Lit ( lit) ) if Self :: check_lit ( lit, 1 ) => {
73
74
Self :: generate_recommendation ( cx, binop, lhslhs, rhs, Side :: Lhs )
74
75
} ,
75
76
_ => None ,
@@ -81,10 +82,10 @@ impl IntPlusOne {
81
82
{
82
83
match ( & rhslhs. kind , & rhsrhs. kind ) {
83
84
// `y + 1` and `1 + y`
84
- ( & ExprKind :: Lit ( ref lit) , _) if Self :: check_lit ( lit, 1 ) => {
85
+ ( & ExprKind :: Lit ( lit) , _) if Self :: check_lit ( lit, 1 ) => {
85
86
Self :: generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: Rhs )
86
87
} ,
87
- ( _, & ExprKind :: Lit ( ref lit) ) if Self :: check_lit ( lit, 1 ) => {
88
+ ( _, & ExprKind :: Lit ( lit) ) if Self :: check_lit ( lit, 1 ) => {
88
89
Self :: generate_recommendation ( cx, binop, rhslhs, lhs, Side :: Rhs )
89
90
} ,
90
91
_ => None ,
@@ -96,10 +97,10 @@ impl IntPlusOne {
96
97
{
97
98
match ( & lhslhs. kind , & lhsrhs. kind ) {
98
99
// `1 + x` and `x + 1`
99
- ( & ExprKind :: Lit ( ref lit) , _) if Self :: check_lit ( lit, 1 ) => {
100
+ ( & ExprKind :: Lit ( lit) , _) if Self :: check_lit ( lit, 1 ) => {
100
101
Self :: generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: Lhs )
101
102
} ,
102
- ( _, & ExprKind :: Lit ( ref lit) ) if Self :: check_lit ( lit, 1 ) => {
103
+ ( _, & ExprKind :: Lit ( lit) ) if Self :: check_lit ( lit, 1 ) => {
103
104
Self :: generate_recommendation ( cx, binop, lhslhs, rhs, Side :: Lhs )
104
105
} ,
105
106
_ => None ,
@@ -109,11 +110,11 @@ impl IntPlusOne {
109
110
( BinOpKind :: Le , _, & ExprKind :: Binary ( ref rhskind, ref rhslhs, ref rhsrhs) ) => {
110
111
match ( rhskind. node , & rhslhs. kind , & rhsrhs. kind ) {
111
112
// `-1 + y`
112
- ( BinOpKind :: Add , & ExprKind :: Lit ( ref lit) , _) if Self :: check_lit ( lit, -1 ) => {
113
+ ( BinOpKind :: Add , & ExprKind :: Lit ( lit) , _) if Self :: check_lit ( lit, -1 ) => {
113
114
Self :: generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: Rhs )
114
115
} ,
115
116
// `y - 1`
116
- ( BinOpKind :: Sub , _, & ExprKind :: Lit ( ref lit) ) if Self :: check_lit ( lit, 1 ) => {
117
+ ( BinOpKind :: Sub , _, & ExprKind :: Lit ( lit) ) if Self :: check_lit ( lit, 1 ) => {
117
118
Self :: generate_recommendation ( cx, binop, rhslhs, lhs, Side :: Rhs )
118
119
} ,
119
120
_ => None ,
0 commit comments