Skip to content

Ignore more type aliases in unnecessary_cast #10927

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

Merged
merged 2 commits into from
Jun 12, 2023
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
29 changes: 27 additions & 2 deletions clippy_lints/src/casts/unnecessary_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion tests/ui/unnecessary_cast.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion tests/ui/unnecessary_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 23 additions & 23 deletions tests/ui/unnecessary_cast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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:116: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:117: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:118: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:119: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:120: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:121: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:122: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:123: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:135: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:136: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:137: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:138: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:139: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:141: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:142: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:146: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:147: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:153: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:159: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:161: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:168: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:170: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:178:20
|
LL | let _num = foo() as f32;
| ^^^^^^^^^^^^ help: try: `foo()`
Expand Down