ignore more type aliases in unnecessary_cast

ignore more type aliases in unnecessary_cast
This commit is contained in:
Centri3 2023-06-11 06:21:31 -05:00
parent f93df98b84
commit 4c7bc1785d
4 changed files with 72 additions and 27 deletions

View File

@ -6,13 +6,14 @@ use if_chain::if_chain;
use rustc_ast::{LitFloatType, LitIntType, LitKind};
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::{Expr, ExprKind, Lit, QPath, TyKind, UnOp};
use rustc_hir::{Expr, ExprKind, Lit, Node, Path, QPath, TyKind, UnOp};
use rustc_lint::{LateContext, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{self, FloatTy, InferTy, Ty};
use super::UNNECESSARY_CAST;
#[expect(clippy::too_many_lines)]
pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>,
expr: &Expr<'tcx>,
@ -58,7 +59,31 @@ pub(super) fn check<'tcx>(
}
}
// skip non-primitive type cast
// skip cast of local to type alias
if let ExprKind::Cast(inner, ..) = expr.kind
&& let ExprKind::Path(qpath) = inner.kind
&& let QPath::Resolved(None, Path { res, .. }) = qpath
&& let Res::Local(hir_id) = res
&& let parent = cx.tcx.hir().get_parent(*hir_id)
&& let Node::Local(local) = parent
{
if let Some(ty) = local.ty
&& let TyKind::Path(qpath) = ty.kind
&& is_ty_alias(&qpath)
{
return false;
}
if let Some(expr) = local.init
&& let ExprKind::Cast(.., cast_to) = expr.kind
&& let TyKind::Path(qpath) = cast_to.kind
&& is_ty_alias(&qpath)
{
return false;
}
}
// skip cast to non-primitive type
if_chain! {
if let ExprKind::Cast(_, cast_to) = expr.kind;
if let TyKind::Path(QPath::Resolved(_, path)) = &cast_to.kind;

View File

@ -66,12 +66,22 @@ fn main() {
foo!(b, f32);
foo!(c, f64);
// do not lint cast from cfg-dependant type
let x = 0 as std::ffi::c_ulong;
let y = x as u64;
let x: std::ffi::c_ulong = 0;
let y = x as u64;
// do not lint cast to cfg-dependant type
1 as std::os::raw::c_char;
let x = 1 as std::os::raw::c_char;
let y = x as u64;
// do not lint cast to alias type
1 as I32Alias;
&1 as &I32Alias;
// or from
let x: I32Alias = 1;
let y = x as u64;
let i8_ptr: *const i8 = &1;
let u8_ptr: *const u8 = &1;

View File

@ -66,12 +66,22 @@ fn main() {
foo!(b, f32);
foo!(c, f64);
// do not lint cast from cfg-dependant type
let x = 0 as std::ffi::c_ulong;
let y = x as u64;
let x: std::ffi::c_ulong = 0;
let y = x as u64;
// do not lint cast to cfg-dependant type
1 as std::os::raw::c_char;
let x = 1 as std::os::raw::c_char;
let y = x as u64;
// do not lint cast to alias type
1 as I32Alias;
&1 as &I32Alias;
// or from
let x: I32Alias = 1;
let y = x as u64;
let i8_ptr: *const i8 = &1;
let u8_ptr: *const u8 = &1;

View File

@ -91,139 +91,139 @@ LL | uwu::<u32, u32>([1u32].as_ptr()) as *const u32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `uwu::<u32, u32>([1u32].as_ptr())`
error: casting integer literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:106:9
--> $DIR/unnecessary_cast.rs:117:9
|
LL | 100 as f32;
| ^^^^^^^^^^ help: try: `100_f32`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:107:9
--> $DIR/unnecessary_cast.rs:118:9
|
LL | 100 as f64;
| ^^^^^^^^^^ help: try: `100_f64`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:108:9
--> $DIR/unnecessary_cast.rs:119:9
|
LL | 100_i32 as f64;
| ^^^^^^^^^^^^^^ help: try: `100_f64`
error: casting integer literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:109:17
--> $DIR/unnecessary_cast.rs:120:17
|
LL | let _ = -100 as f32;
| ^^^^^^^^^^^ help: try: `-100_f32`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:110:17
--> $DIR/unnecessary_cast.rs:121:17
|
LL | let _ = -100 as f64;
| ^^^^^^^^^^^ help: try: `-100_f64`
error: casting integer literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:111:17
--> $DIR/unnecessary_cast.rs:122:17
|
LL | let _ = -100_i32 as f64;
| ^^^^^^^^^^^^^^^ help: try: `-100_f64`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:112:9
--> $DIR/unnecessary_cast.rs:123:9
|
LL | 100. as f32;
| ^^^^^^^^^^^ help: try: `100_f32`
error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:113:9
--> $DIR/unnecessary_cast.rs:124:9
|
LL | 100. as f64;
| ^^^^^^^^^^^ help: try: `100_f64`
error: casting integer literal to `u32` is unnecessary
--> $DIR/unnecessary_cast.rs:125:9
--> $DIR/unnecessary_cast.rs:136:9
|
LL | 1 as u32;
| ^^^^^^^^ help: try: `1_u32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:126:9
--> $DIR/unnecessary_cast.rs:137:9
|
LL | 0x10 as i32;
| ^^^^^^^^^^^ help: try: `0x10_i32`
error: casting integer literal to `usize` is unnecessary
--> $DIR/unnecessary_cast.rs:127:9
--> $DIR/unnecessary_cast.rs:138:9
|
LL | 0b10 as usize;
| ^^^^^^^^^^^^^ help: try: `0b10_usize`
error: casting integer literal to `u16` is unnecessary
--> $DIR/unnecessary_cast.rs:128:9
--> $DIR/unnecessary_cast.rs:139:9
|
LL | 0o73 as u16;
| ^^^^^^^^^^^ help: try: `0o73_u16`
error: casting integer literal to `u32` is unnecessary
--> $DIR/unnecessary_cast.rs:129:9
--> $DIR/unnecessary_cast.rs:140:9
|
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.rs:131:9
--> $DIR/unnecessary_cast.rs:142:9
|
LL | 1.0 as f64;
| ^^^^^^^^^^ help: try: `1.0_f64`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:132:9
--> $DIR/unnecessary_cast.rs:143:9
|
LL | 0.5 as f32;
| ^^^^^^^^^^ help: try: `0.5_f32`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:136:17
--> $DIR/unnecessary_cast.rs:147:17
|
LL | let _ = -1 as i32;
| ^^^^^^^^^ help: try: `-1_i32`
error: casting float literal to `f32` is unnecessary
--> $DIR/unnecessary_cast.rs:137:17
--> $DIR/unnecessary_cast.rs:148:17
|
LL | let _ = -1.0 as f32;
| ^^^^^^^^^^^ help: try: `-1.0_f32`
error: casting to the same type is unnecessary (`i32` -> `i32`)
--> $DIR/unnecessary_cast.rs:143:18
--> $DIR/unnecessary_cast.rs:154:18
|
LL | let _ = &(x as i32);
| ^^^^^^^^^^ help: try: `{ x }`
error: casting integer literal to `i32` is unnecessary
--> $DIR/unnecessary_cast.rs:149:22
--> $DIR/unnecessary_cast.rs:160:22
|
LL | let _: i32 = -(1) as i32;
| ^^^^^^^^^^^ help: try: `-1_i32`
error: casting integer literal to `i64` is unnecessary
--> $DIR/unnecessary_cast.rs:151:22
--> $DIR/unnecessary_cast.rs:162:22
|
LL | let _: i64 = -(1) as i64;
| ^^^^^^^^^^^ help: try: `-1_i64`
error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:158:22
--> $DIR/unnecessary_cast.rs:169:22
|
LL | let _: f64 = (-8.0 as f64).exp();
| ^^^^^^^^^^^^^ help: try: `(-8.0_f64)`
error: casting float literal to `f64` is unnecessary
--> $DIR/unnecessary_cast.rs:160:23
--> $DIR/unnecessary_cast.rs:171:23
|
LL | let _: f64 = -(8.0 as f64).exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
| ^^^^^^^^^^^^ help: try: `8.0_f64`
error: casting to the same type is unnecessary (`f32` -> `f32`)
--> $DIR/unnecessary_cast.rs:168:20
--> $DIR/unnecessary_cast.rs:179:20
|
LL | let _num = foo() as f32;
| ^^^^^^^^^^^^ help: try: `foo()`