Skip to content

Commit 80cfbd0

Browse files
committed
Update lints and tests
1 parent e61f6b6 commit 80cfbd0

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

clippy_lints/src/xor_used_as_pow.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ declare_lint_pass!(XorUsedAsPow => [XOR_USED_AS_POW]);
4343

4444
impl LateLintPass<'_> for XorUsedAsPow {
4545
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-
5246
let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id);
5347
if let Some(Node::Item(parent_item)) = cx.tcx.hir().find(parent_id) {
5448
if let ItemKind::Enum(_, _) = parent_item.kind {
@@ -128,7 +122,7 @@ fn report_with_ident(cx: &LateContext<'_>, lhs: u128, rhs: &QPath<'_>, span: Spa
128122
match lhs {
129123
2 => {
130124
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);
132126
},
133127
10 => report_pow_of_ten(cx, span),
134128
_ => {},
@@ -141,6 +135,11 @@ fn report_with_lit(cx: &LateContext<'_>, lhs: u128, rhs: u128, span: Span) {
141135
}
142136
match lhs {
143137
2 => {
138+
if rhs == 0 {
139+
report_pow_of_two(cx, format!("1"), span);
140+
return;
141+
}
142+
144143
let lhs_str = if rhs <= 31 {
145144
"1_u32"
146145
} else if rhs <= 63 {
@@ -149,21 +148,21 @@ fn report_with_lit(cx: &LateContext<'_>, lhs: u128, rhs: u128, span: Span) {
149148
"1_u127"
150149
};
151150

152-
report_pow_of_two(cx, lhs_str, &rhs.to_string(), span);
151+
report_pow_of_two(cx, format!("{} << {}", lhs_str, rhs), span);
153152
},
154153
10 => report_pow_of_ten(cx, span),
155154
_ => {},
156155
}
157156
}
158157

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) {
160159
span_lint_and_sugg(
161160
cx,
162161
XOR_USED_AS_POW,
163162
span,
164163
"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,
167166
Applicability::MaybeIncorrect,
168167
)
169168
}

tests/ui/xor_used_as_pow.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: it appears you are trying to get a power of two, but `^` is not an expone
22
--> $DIR/xor_used_as_pow.rs:22:13
33
|
44
LL | let _ = 2 ^ 3;
5-
| ^^^^^ help: use a bitshift instead: `1_u32 << 3`
5+
| ^^^^^ help: use a bitshift or constant instead: `1_u32 << 3`
66
|
77
= note: `-D clippy::xor-used-as-pow` implied by `-D warnings`
88

@@ -18,7 +18,7 @@ error: it appears you are trying to get a power of two, but `^` is not an expone
1818
--> $DIR/xor_used_as_pow.rs:24:13
1919
|
2020
LL | let _ = 2 ^ 32;
21-
| ^^^^^^ help: use a bitshift instead: `1_u64 << 32`
21+
| ^^^^^^ help: use a bitshift or constant instead: `1_u64 << 32`
2222

2323
error: the operation is ineffective. Consider reducing it to `2`
2424
--> $DIR/xor_used_as_pow.rs:25:13
@@ -32,7 +32,7 @@ error: it appears you are trying to get a power of two, but `^` is not an expone
3232
--> $DIR/xor_used_as_pow.rs:25:13
3333
|
3434
LL | let _ = 2 ^ 0;
35-
| ^^^^^ help: try: `1`
35+
| ^^^^^ help: use a bitshift or constant instead: `1`
3636

3737
error: the operation is ineffective. Consider reducing it to `10`
3838
--> $DIR/xor_used_as_pow.rs:26:13
@@ -48,19 +48,19 @@ LL | let _ = 10 ^ 0;
4848
|
4949
= help: did you mean to use .pow()?
5050

51-
error: aborting due to 7 previous errors
52-
5351
error: it appears you are trying to get a power of two, but `^` is not an exponentiation operator
5452
--> $DIR/xor_used_as_pow.rs:29:17
5553
|
5654
LL | let _ = 2 ^ x;
57-
| ^^^^ help: use a bitshift instead: `1_u32 << 15`
55+
| ^^^^^ help: use a bitshift or constant instead: `1 << x`
5856

5957
error: `^` is not an exponentiation operator but appears to have been used as one
6058
--> $DIR/xor_used_as_pow.rs:30:17
6159
|
6260
LL | let _ = 10 ^ x;
63-
| ^^^^^
61+
| ^^^^^^
6462
|
6563
= help: did you mean to use .pow()?
6664

65+
error: aborting due to 9 previous errors
66+

0 commit comments

Comments
 (0)