Skip to content

Clippy fix doesn't add required parentheses to cast #9391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ijijn opened this issue Aug 29, 2022 · 0 comments · Fixed by #9394
Closed

Clippy fix doesn't add required parentheses to cast #9391

ijijn opened this issue Aug 29, 2022 · 0 comments · Fixed by #9394
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@ijijn
Copy link
Contributor

ijijn commented Aug 29, 2022

Summary

Hi team!

Clippy fix fails to add parentheses around an existing cast when the newer code requires it, resulting in an error and subsequent rollback. It's similar to #9380, but kind of the opposite. 😄

Reproducer

I tried this code with cargo clippy --fix:

#![warn(clippy::nursery)]

#[allow(dead_code)]
fn radians(degrees: i64) -> f64 {
    degrees as f64 * std::f64::consts::PI / 180.0
}

I expected to see this happen:

#![warn(clippy::nursery)]

#[allow(dead_code)]
fn radians(degrees: i64) -> f64 {
    (degrees as f64).to_radians()
}

Instead, this happened:

warning: failed to automatically apply fixes suggested by rustc to crate `clipsville`

  |     ^^^^^^^^^^^^^^
  |
help: try surrounding the expression in parentheses
  |
5 |     (degrees as f64).to_radians()
  |     +              +

error: aborting due to previous error

Original diagnostics will follow.

warning: conversion to radians can be done more accurately
 --> src\lib.rs:5:5
  |
5 |     degrees as f64 * std::f64::consts::PI / 180.0
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `degrees as f64.to_radians()`
  |
note: the lint level is defined here
 --> src\lib.rs:1:9
  |
1 | #![warn(clippy::nursery)]
  |         ^^^^^^^^^^^^^^^
  = note: `#[warn(clippy::suboptimal_flops)]` implied by `#[warn(clippy::nursery)]`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flops

warning: `clipsville` (lib) generated 1 warning
warning: `clipsville` (lib test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 1.23s

Version

rustc 1.65.0-nightly (eaadb8947 2022-08-27)
binary: rustc
commit-hash: eaadb8947b850a025404082f6297766c4680a42a
commit-date: 2022-08-27
host: x86_64-pc-windows-msvc
release: 1.65.0-nightly
LLVM version: 15.0.0

Additional Labels

No response

@ijijn ijijn added the C-bug Category: Clippy is not doing the correct thing label Aug 29, 2022
@Alexendoo Alexendoo added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Aug 29, 2022
bors added a commit that referenced this issue Aug 30, 2022
Fix missing parens in `suboptimal_flops` suggestion

Fixes #9391. The problem is simple enough, I didn't check if the same problem occurs elsewhere, though.

changelog: fix missing parenthesis in `suboptimal_flops` suggestion
@bors bors closed this as completed in 26a6891 Aug 30, 2022
kartva pushed a commit to kartva/rust-clippy that referenced this issue Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants