rust-clippy/tests/ui/unnecessary_cast.fixed

114 lines
2.1 KiB
Rust

// run-rustfix
#![warn(clippy::unnecessary_cast)]
#![allow(
unused_must_use,
clippy::borrow_as_ptr,
clippy::no_effect,
clippy::nonstandard_macro_braces,
clippy::unnecessary_operation
)]
#[rustfmt::skip]
fn main() {
// Test cast_unnecessary
1_i32;
1_f32;
false;
&1i32 as &i32;
-1_i32;
- 1_i32;
-1_f32;
1_i32;
1_f32;
// macro version
macro_rules! foo {
($a:ident, $b:ident) => {
#[allow(unused)]
pub fn $a() -> $b {
1 as $b
}
};
}
foo!(a, i32);
foo!(b, f32);
foo!(c, f64);
// do not lint cast to cfg-dependant type
1 as std::os::raw::c_char;
// do not lint cast to alias type
1 as I32Alias;
&1 as &I32Alias;
}
type I32Alias = i32;
mod fixable {
#![allow(dead_code)]
fn main() {
// casting integer literal to float is unnecessary
100_f32;
100_f64;
100_f64;
let _ = -100_f32;
let _ = -100_f64;
let _ = -100_f64;
100_f32;
100_f64;
// Should not trigger
#[rustfmt::skip]
let v = vec!(1);
&v as &[i32];
0x10 as f32;
0o10 as f32;
0b10 as f32;
0x11 as f64;
0o11 as f64;
0b11 as f64;
1_u32;
0x10_i32;
0b10_usize;
0o73_u16;
1_000_000_000_u32;
1.0_f64;
0.5_f32;
1.0 as u16;
let _ = -1_i32;
let _ = -1.0_f32;
let _ = 1 as I32Alias;
let _ = &1 as &I32Alias;
}
type I32Alias = i32;
fn issue_9380() {
let _: i32 = -1_i32;
let _: f32 = -(1) as f32;
let _: i64 = -1_i64;
let _: i64 = -(1.0) as i64;
let _ = -(1 + 1) as i64;
}
fn issue_9563() {
let _: f64 = (-8.0_f64).exp();
#[allow(clippy::precedence)]
let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
}
fn issue_9562_non_literal() {
fn foo() -> f32 {
0.
}
let _num = foo();
}
}