@@ -43,12 +43,6 @@ declare_lint_pass!(XorUsedAsPow => [XOR_USED_AS_POW]);
43
43
44
44
impl LateLintPass < ' _ > for XorUsedAsPow {
45
45
fn check_expr ( & mut self , cx : & LateContext < ' _ > , expr : & ' _ Expr < ' _ > ) {
46
- // TODO:
47
- // [ ] catch statements with literal or constant variables in rhs
48
- // [x] check for overflows on suggested changes
49
- // [x] ignore binary, octal, and hex literals in either side
50
- // [x] avoid linting on enum discriminants
51
-
52
46
let parent_id = cx. tcx . hir ( ) . get_parent_item ( expr. hir_id ) ;
53
47
if let Some ( Node :: Item ( parent_item) ) = cx. tcx . hir ( ) . find ( parent_id) {
54
48
if let ItemKind :: Enum ( _, _) = parent_item. kind {
@@ -128,7 +122,7 @@ fn report_with_ident(cx: &LateContext<'_>, lhs: u128, rhs: &QPath<'_>, span: Spa
128
122
match lhs {
129
123
2 => {
130
124
let ident = last_path_segment ( rhs) . ident . name . to_ident_string ( ) ;
131
- report_pow_of_two ( cx, "1 ", & ident, span) ;
125
+ report_pow_of_two ( cx, format ! ( "1 << {} ", ident) , span) ;
132
126
} ,
133
127
10 => report_pow_of_ten ( cx, span) ,
134
128
_ => { } ,
@@ -141,6 +135,11 @@ fn report_with_lit(cx: &LateContext<'_>, lhs: u128, rhs: u128, span: Span) {
141
135
}
142
136
match lhs {
143
137
2 => {
138
+ if rhs == 0 {
139
+ report_pow_of_two ( cx, format ! ( "1" ) , span) ;
140
+ return ;
141
+ }
142
+
144
143
let lhs_str = if rhs <= 31 {
145
144
"1_u32"
146
145
} else if rhs <= 63 {
@@ -149,21 +148,21 @@ fn report_with_lit(cx: &LateContext<'_>, lhs: u128, rhs: u128, span: Span) {
149
148
"1_u127"
150
149
} ;
151
150
152
- report_pow_of_two ( cx, lhs_str, & rhs. to_string ( ) , span) ;
151
+ report_pow_of_two ( cx, format ! ( "{} << {}" , lhs_str, rhs) , span) ;
153
152
} ,
154
153
10 => report_pow_of_ten ( cx, span) ,
155
154
_ => { } ,
156
155
}
157
156
}
158
157
159
- fn report_pow_of_two ( cx : & LateContext < ' _ > , lhs : & str , rhs : & str , span : Span ) {
158
+ fn report_pow_of_two ( cx : & LateContext < ' _ > , sugg : String , span : Span ) {
160
159
span_lint_and_sugg (
161
160
cx,
162
161
XOR_USED_AS_POW ,
163
162
span,
164
163
"it appears you are trying to get a power of two, but `^` is not an exponentiation operator" ,
165
- "use a bitshift instead" ,
166
- format ! ( "{} << {}" , lhs , rhs ) ,
164
+ "use a bitshift or constant instead" ,
165
+ sugg ,
167
166
Applicability :: MaybeIncorrect ,
168
167
)
169
168
}
0 commit comments