rust-lang / rust-lang/rust-clippy
log base can be expressed more clearly
Open
Nobody has claimed this yet.
C-enhancement
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Description
pub fn calc(x: i32, y: i32) -> i32 {
let y = f64::from(y);
let x = f64::from(x);
(y.log2() / x.log2()).ceil() as i32
}
pub fn calc2(x: i32, y: i32) -> i32 {
let y = f64::from(y);
let x = f64::from(x);
y.log(x).ceil() as i32
}
fn main() {
println!("{} {}", calc(5, 125), calc2(5, 125)); // 3 4
}
run the above code with cippy will give clippy::suboptimal-flops error, but the expression suggest by clippy is not equivalent to original one
error: log base can be expressed more clearly
|
4 | (y.log2() / x.log2()).ceil() as i32
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `y.log(x)`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flops
= note: `-D clippy::suboptimal-flops` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::suboptimal_flops)]`
Version
rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2
Additional Labels
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the Rust reproduction with the suboptimal-flops lint enabled and compare calc with the suggested y.log(x) expression. Trace the suboptimal-flops lint entry point and identify where the suggestion is generated. Done means the lint no longer recommends a non-equivalent expression and the regression is covered by an appropriate test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100