Skip to content

Commit ef8454e

Browse files
committed
Emit lint on literal for consistency and precise hint
1 parent e338829 commit ef8454e

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

clippy_lints/src/decimal_bit_mask.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::source::SpanRangeExt;
3+
use rustc_ast::LitKind;
34
use rustc_data_structures::packed::Pu128;
45
use rustc_hir::{AssignOpKind, BinOpKind, Expr, ExprKind};
56
use rustc_lint::{LateContext, LateLintPass};
@@ -42,11 +43,11 @@ fn check_bitwise_binary_expr(cx: &LateContext<'_>, e: &Expr<'_>) {
4243
) = &e.kind
4344
{
4445
for expr in [left, right] {
45-
if let ExprKind::Lit(_) = &expr.kind
46-
&& !is_not_decimal_number(cx, expr.span)
47-
&& !is_power_of_twoish(expr)
46+
if let ExprKind::Lit(lit) = &expr.kind
47+
&& !is_not_decimal_number(cx, lit.span)
48+
&& !is_power_of_twoish(lit)
4849
{
49-
emit_lint(cx, expr.span);
50+
emit_lint(cx, lit.span);
5051
}
5152
}
5253
}
@@ -59,12 +60,12 @@ fn check_bitwise_assign_expr(cx: &LateContext<'_>, e: &Expr<'_>) {
5960
..
6061
},
6162
_,
62-
expr @ Expr {
63+
Expr {
6364
kind: ExprKind::Lit(lit),
6465
..
6566
},
6667
) = &e.kind
67-
&& !is_power_of_twoish(expr)
68+
&& !is_power_of_twoish(lit)
6869
&& !is_not_decimal_number(cx, lit.span)
6970
{
7071
emit_lint(cx, lit.span);
@@ -75,10 +76,8 @@ fn is_not_decimal_number(cx: &LateContext<'_>, span: Span) -> bool {
7576
span.check_source_text(cx, |src| src.contains("0b") || src.contains("0x") || src.contains("0o"))
7677
}
7778

78-
fn is_power_of_twoish(expr: &Expr<'_>) -> bool {
79-
if let ExprKind::Lit(lit) = &expr.kind
80-
&& let rustc_ast::LitKind::Int(Pu128(val), _) = lit.node
81-
{
79+
fn is_power_of_twoish(lit: &Spanned<LitKind>) -> bool {
80+
if let LitKind::Int(Pu128(val), _) = lit.node {
8281
return val.is_power_of_two() || val.wrapping_add(1).is_power_of_two();
8382
}
8483
false

tests/ui/decimal_bit_mask.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ fn main() {
2222

2323
// BAD: Bitwise operation, decimal literal, two literals
2424
0b1010 & 99; //~ decimal_bit_mask
25-
0b1010 | 99; //~ decimal_bit_mask
26-
0b1010 ^ 99; //~ decimal_bit_mask
25+
0b1010 | (99); //~ decimal_bit_mask
26+
0b1010 ^ (/* comment */99); //~ decimal_bit_mask
2727
99 & 0b1010; //~ decimal_bit_mask
28-
99 | 0b1010; //~ decimal_bit_mask
29-
99 ^ 0b1010; //~ decimal_bit_mask
28+
(99) | 0b1010; //~ decimal_bit_mask
29+
(/* comment */99) ^ 0b1010; //~ decimal_bit_mask
3030
0xD | 99; //~ decimal_bit_mask
3131
88 & 99;
3232
//~^ decimal_bit_mask
@@ -84,7 +84,7 @@ fn main() {
8484

8585
// GOOD: Power of two and power of two minus one
8686
x & 16; // 2^4
87-
x | 31; // 2^5 - 1
87+
x | (31); // 2^5 - 1
8888
x ^ 0x40; // 2^6 (hex)
8989
x ^= 7; // 2^3 - 1
9090

tests/ui/decimal_bit_mask.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ LL | x & 99;
99
= help: to override `-D warnings` add `#[allow(clippy::decimal_bit_mask)]`
1010

1111
error: using decimal literal for bitwise operation
12-
--> tests/ui/decimal_bit_mask.rs:17:9
12+
--> tests/ui/decimal_bit_mask.rs:17:23
1313
|
1414
LL | x | (/* comment */99);
15-
| ^^^^^^^^^^^^^^^^^
15+
| ^^
1616
|
1717
= help: use binary (0b...) or hex (0x...) or octal (0o...) notation for better readability
1818

1919
error: using decimal literal for bitwise operation
20-
--> tests/ui/decimal_bit_mask.rs:18:9
20+
--> tests/ui/decimal_bit_mask.rs:18:10
2121
|
2222
LL | x ^ (99);
23-
| ^^^^
23+
| ^^
2424
|
2525
= help: use binary (0b...) or hex (0x...) or octal (0o...) notation for better readability
2626

@@ -57,18 +57,18 @@ LL | 0b1010 & 99;
5757
= help: use binary (0b...) or hex (0x...) or octal (0o...) notation for better readability
5858

5959
error: using decimal literal for bitwise operation
60-
--> tests/ui/decimal_bit_mask.rs:25:14
60+
--> tests/ui/decimal_bit_mask.rs:25:15
6161
|
62-
LL | 0b1010 | 99;
63-
| ^^
62+
LL | 0b1010 | (99);
63+
| ^^
6464
|
6565
= help: use binary (0b...) or hex (0x...) or octal (0o...) notation for better readability
6666

6767
error: using decimal literal for bitwise operation
68-
--> tests/ui/decimal_bit_mask.rs:26:14
68+
--> tests/ui/decimal_bit_mask.rs:26:28
6969
|
70-
LL | 0b1010 ^ 99;
71-
| ^^
70+
LL | 0b1010 ^ (/* comment */99);
71+
| ^^
7272
|
7373
= help: use binary (0b...) or hex (0x...) or octal (0o...) notation for better readability
7474

@@ -81,18 +81,18 @@ LL | 99 & 0b1010;
8181
= help: use binary (0b...) or hex (0x...) or octal (0o...) notation for better readability
8282

8383
error: using decimal literal for bitwise operation
84-
--> tests/ui/decimal_bit_mask.rs:28:5
84+
--> tests/ui/decimal_bit_mask.rs:28:6
8585
|
86-
LL | 99 | 0b1010;
87-
| ^^
86+
LL | (99) | 0b1010;
87+
| ^^
8888
|
8989
= help: use binary (0b...) or hex (0x...) or octal (0o...) notation for better readability
9090

9191
error: using decimal literal for bitwise operation
92-
--> tests/ui/decimal_bit_mask.rs:29:5
92+
--> tests/ui/decimal_bit_mask.rs:29:19
9393
|
94-
LL | 99 ^ 0b1010;
95-
| ^^
94+
LL | (/* comment */99) ^ 0b1010;
95+
| ^^
9696
|
9797
= help: use binary (0b...) or hex (0x...) or octal (0o...) notation for better readability
9898

0 commit comments

Comments
 (0)