Skip to content

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

Closed
@ijijn

Description

@ijijn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions