Skip to content

Commit be50786

Browse files
committed
Refine suggestion
1 parent 53b0074 commit be50786

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

clippy_lints/src/methods/inefficient_pow.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, recv:
2929
LitIntType::Unsigned(int) => int.name_str(),
3030
LitIntType::Unsuffixed => "",
3131
};
32+
// `lhs * 1` is useless
33+
let rhs = if power_used == 1 {
34+
arg_snippet.to_string()
35+
} else {
36+
format!("({arg_snippet} * {power_used})")
37+
};
3238

3339
span_lint_and_sugg(
3440
cx,
3541
INEFFICIENT_POW,
3642
expr.span,
3743
"inefficient `.pow()`",
3844
"use `<<` instead",
39-
format!("1{suffix} << ({arg_snippet} * {power_used})"),
45+
format!("1{suffix} << {rhs}"),
4046
Applicability::MaybeIncorrect,
4147
);
4248
}

tests/ui/inefficient_pow.fixed

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
extern crate proc_macros;
1414

1515
fn main() {
16-
_ = 1i32 << (1 * 1);
17-
_ = 1i32 << (0 * 1);
18-
_ = 1i32 << (3 * 1);
19-
_ = 1i32 << (100 * 1);
16+
_ = 1i32 << 1;
17+
_ = 1i32 << 0;
18+
_ = 1i32 << 3;
19+
_ = 1i32 << 100;
2020
_ = 1i32 << (3 * 2);
2121
_ = 1i32 << (3 * 5);
2222
_ = 1i32 << (3 * 6);

tests/ui/inefficient_pow.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ error: inefficient `.pow()`
22
--> $DIR/inefficient_pow.rs:16:9
33
|
44
LL | _ = 2i32.pow(1);
5-
| ^^^^^^^^^^^ help: use `<<` instead: `1i32 << (1 * 1)`
5+
| ^^^^^^^^^^^ help: use `<<` instead: `1i32 << 1`
66
|
77
= note: `-D clippy::inefficient-pow` implied by `-D warnings`
88

99
error: inefficient `.pow()`
1010
--> $DIR/inefficient_pow.rs:17:9
1111
|
1212
LL | _ = 2i32.pow(0);
13-
| ^^^^^^^^^^^ help: use `<<` instead: `1i32 << (0 * 1)`
13+
| ^^^^^^^^^^^ help: use `<<` instead: `1i32 << 0`
1414

1515
error: inefficient `.pow()`
1616
--> $DIR/inefficient_pow.rs:18:9
1717
|
1818
LL | _ = 2i32.pow(3);
19-
| ^^^^^^^^^^^ help: use `<<` instead: `1i32 << (3 * 1)`
19+
| ^^^^^^^^^^^ help: use `<<` instead: `1i32 << 3`
2020

2121
error: inefficient `.pow()`
2222
--> $DIR/inefficient_pow.rs:19:9
2323
|
2424
LL | _ = 2i32.pow(100);
25-
| ^^^^^^^^^^^^^ help: use `<<` instead: `1i32 << (100 * 1)`
25+
| ^^^^^^^^^^^^^ help: use `<<` instead: `1i32 << 100`
2626

2727
error: inefficient `.pow()`
2828
--> $DIR/inefficient_pow.rs:20:9

0 commit comments

Comments
 (0)