Skip to content
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

Fix rust-lang/rust#79255 - Incorrect try suggestion for float cast #6362

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1711,7 +1711,7 @@ fn show_unnecessary_cast(cx: &LateContext<'_>, expr: &Expr<'_>, literal_str: &st
expr.span,
&format!("casting {} literal to `{}` is unnecessary", literal_kind_name, cast_to),
"try",
format!("{}_{}", literal_str, cast_to),
format!("{}_{}", literal_str.trim_end_matches('.'), cast_to),
Applicability::MachineApplicable,
);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/unnecessary_cast_fixable.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ fn main() {
let _ = -100_f32;
let _ = -100_f64;
let _ = -100_f64;
100_f32;
100_f64;
// Should not trigger
#[rustfmt::skip]
let v = vec!(1);
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/unnecessary_cast_fixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ fn main() {
let _ = -100 as f32;
let _ = -100 as f64;
let _ = -100_i32 as f64;
100. as f32;
100. as f64;
// Should not trigger
#[rustfmt::skip]
let v = vec!(1);
Expand Down
32 changes: 22 additions & 10 deletions tests/ui/unnecessary_cast_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -36,59 +36,71 @@ error: casting integer literal to `f64` is unnecessary
LL | let _ = -100_i32 as f64;
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`

error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:14:5
|
LL | 100. as f32;
| ^^^^^^^^^^^ help: try: `100_f32`

error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:15:5
|
LL | 100. as f64;
| ^^^^^^^^^^^ help: try: `100_f64`

error: casting integer literal to `u32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:25:5
--> $DIR/unnecessary_cast_fixable.rs:27:5
|
LL | 1 as u32;
| ^^^^^^^^ help: try: `1_u32`

error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:26:5
--> $DIR/unnecessary_cast_fixable.rs:28:5
|
LL | 0x10 as i32;
| ^^^^^^^^^^^ help: try: `0x10_i32`

error: casting integer literal to `usize` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:27:5
--> $DIR/unnecessary_cast_fixable.rs:29:5
|
LL | 0b10 as usize;
| ^^^^^^^^^^^^^ help: try: `0b10_usize`

error: casting integer literal to `u16` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:28:5
--> $DIR/unnecessary_cast_fixable.rs:30:5
|
LL | 0o73 as u16;
| ^^^^^^^^^^^ help: try: `0o73_u16`

error: casting integer literal to `u32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:29:5
--> $DIR/unnecessary_cast_fixable.rs:31:5
|
LL | 1_000_000_000 as u32;
| ^^^^^^^^^^^^^^^^^^^^ help: try: `1_000_000_000_u32`

error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:31:5
--> $DIR/unnecessary_cast_fixable.rs:33:5
|
LL | 1.0 as f64;
| ^^^^^^^^^^ help: try: `1.0_f64`

error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:32:5
--> $DIR/unnecessary_cast_fixable.rs:34:5
|
LL | 0.5 as f32;
| ^^^^^^^^^^ help: try: `0.5_f32`

error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:36:13
--> $DIR/unnecessary_cast_fixable.rs:38:13
|
LL | let _ = -1 as i32;
| ^^^^^^^^^ help: try: `-1_i32`

error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast_fixable.rs:37:13
--> $DIR/unnecessary_cast_fixable.rs:39:13
|
LL | let _ = -1.0 as f32;
| ^^^^^^^^^^^ help: try: `-1.0_f32`

error: aborting due to 15 previous errors
error: aborting due to 17 previous errors