22
33use clippy_utils:: diagnostics:: span_lint_and_sugg;
44use 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;
67use rustc_errors:: Applicability ;
78use rustc_lint:: { EarlyContext , EarlyLintPass } ;
89use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -52,8 +53,8 @@ enum Side {
5253
5354impl IntPlusOne {
5455 #[ 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 ) {
5758 return value == ( target_value as u128 ) ;
5859 }
5960 false
@@ -65,11 +66,11 @@ impl IntPlusOne {
6566 ( BinOpKind :: Ge , & ExprKind :: Binary ( ref lhskind, ref lhslhs, ref lhsrhs) , _) => {
6667 match ( lhskind. node , & lhslhs. kind , & lhsrhs. kind ) {
6768 // `-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 ) => {
6970 Self :: generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: Lhs )
7071 } ,
7172 // `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 ) => {
7374 Self :: generate_recommendation ( cx, binop, lhslhs, rhs, Side :: Lhs )
7475 } ,
7576 _ => None ,
@@ -81,10 +82,10 @@ impl IntPlusOne {
8182 {
8283 match ( & rhslhs. kind , & rhsrhs. kind ) {
8384 // `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 ) => {
8586 Self :: generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: Rhs )
8687 } ,
87- ( _, & ExprKind :: Lit ( ref lit) ) if Self :: check_lit ( lit, 1 ) => {
88+ ( _, & ExprKind :: Lit ( lit) ) if Self :: check_lit ( lit, 1 ) => {
8889 Self :: generate_recommendation ( cx, binop, rhslhs, lhs, Side :: Rhs )
8990 } ,
9091 _ => None ,
@@ -96,10 +97,10 @@ impl IntPlusOne {
9697 {
9798 match ( & lhslhs. kind , & lhsrhs. kind ) {
9899 // `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 ) => {
100101 Self :: generate_recommendation ( cx, binop, lhsrhs, rhs, Side :: Lhs )
101102 } ,
102- ( _, & ExprKind :: Lit ( ref lit) ) if Self :: check_lit ( lit, 1 ) => {
103+ ( _, & ExprKind :: Lit ( lit) ) if Self :: check_lit ( lit, 1 ) => {
103104 Self :: generate_recommendation ( cx, binop, lhslhs, rhs, Side :: Lhs )
104105 } ,
105106 _ => None ,
@@ -109,11 +110,11 @@ impl IntPlusOne {
109110 ( BinOpKind :: Le , _, & ExprKind :: Binary ( ref rhskind, ref rhslhs, ref rhsrhs) ) => {
110111 match ( rhskind. node , & rhslhs. kind , & rhsrhs. kind ) {
111112 // `-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 ) => {
113114 Self :: generate_recommendation ( cx, binop, rhsrhs, lhs, Side :: Rhs )
114115 } ,
115116 // `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 ) => {
117118 Self :: generate_recommendation ( cx, binop, rhslhs, lhs, Side :: Rhs )
118119 } ,
119120 _ => None ,
0 commit comments