Skip to content

Commit a5bc149

Browse files
committed
Refine suggestion
1 parent 53b0074 commit a5bc149

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

clippy_lints/src/methods/inefficient_pow.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use clippy_utils::{diagnostics::span_lint_and_sugg, is_from_proc_macro, source::snippet_opt};
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_from_proc_macro;
3+
use clippy_utils::source::snippet_opt;
24
use rustc_ast::{LitIntType, LitKind};
35
use rustc_errors::Applicability;
46
use rustc_hir::{Expr, ExprKind};
@@ -29,14 +31,20 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, recv:
2931
LitIntType::Unsigned(int) => int.name_str(),
3032
LitIntType::Unsuffixed => "",
3133
};
34+
// `lhs * 1` is useless
35+
let rhs = if power_used == 1 {
36+
arg_snippet.to_string()
37+
} else {
38+
format!("({arg_snippet} * {power_used})")
39+
};
3240

3341
span_lint_and_sugg(
3442
cx,
3543
INEFFICIENT_POW,
3644
expr.span,
3745
"inefficient `.pow()`",
3846
"use `<<` instead",
39-
format!("1{suffix} << ({arg_snippet} * {power_used})"),
47+
format!("1{suffix} << {rhs}"),
4048
Applicability::MaybeIncorrect,
4149
);
4250
}

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)