Skip to content

Check for fully qualified paths in unnecessary_cast #10971

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
Jul 20, 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
8 changes: 8 additions & 0 deletions clippy_lints/src/casts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ declare_clippy_lint! {
/// ### Why is this bad?
/// It's just unnecessary.
///
/// ### Known problems
/// When the expression on the left is a function call, the lint considers the return type to be
/// a type alias if it's aliased through a `use` statement
/// (like `use std::io::Result as IoResult`). It will not lint such cases.
///
/// This check is also rather primitive. It will only work on primitive types without any
/// intermediate references, raw pointers and trait objects may or may not work.
///
/// ### Example
/// ```rust
/// let _ = 2i32 as i32;
Expand Down
22 changes: 12 additions & 10 deletions clippy_lints/src/casts/unnecessary_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ pub(super) fn check<'tcx>(
}
}

// skip cast of fn call that returns type alias
if let ExprKind::Cast(inner, ..) = expr.kind && is_cast_from_ty_alias(cx, inner, cast_from) {
return false;
}

// skip cast to non-primitive type
if_chain! {
if let ExprKind::Cast(_, cast_to) = expr.kind;
Expand All @@ -101,6 +96,11 @@ pub(super) fn check<'tcx>(
}
}

// skip cast of fn call that returns type alias
if let ExprKind::Cast(inner, ..) = expr.kind && is_cast_from_ty_alias(cx, inner, cast_from) {
return false;
}

if let Some(lit) = get_numeric_literal(cast_expr) {
let literal_str = &cast_str;

Expand Down Expand Up @@ -254,14 +254,12 @@ fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx
// function's declaration snippet is exactly equal to the `Ty`. That way, we can
// see whether it's a type alias.
//
// Will this work for more complex types? Probably not!
// FIXME: This won't work if the type is given an alias through `use`, should we
// consider this a type alias as well?
if !snippet
.split("->")
.skip(1)
.map(|s| {
s.trim() == cast_from.to_string()
|| s.split("where").any(|ty| ty.trim() == cast_from.to_string())
})
.map(|s| snippet_eq_ty(s, cast_from) || s.split("where").any(|ty| snippet_eq_ty(ty, cast_from)))
.any(|a| a)
{
return ControlFlow::Break(());
Expand All @@ -288,3 +286,7 @@ fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx
})
.is_some()
}

fn snippet_eq_ty(snippet: &str, ty: Ty<'_>) -> bool {
snippet.trim() == ty.to_string() || snippet.trim().contains(&format!("::{ty}"))
}
17 changes: 17 additions & 0 deletions tests/ui/unnecessary_cast.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ mod fake_libc {
}
}

fn aaa() -> ::std::primitive::u32 {
0
}

use std::primitive::u32 as UnsignedThirtyTwoBitInteger;

fn bbb() -> UnsignedThirtyTwoBitInteger {
0
}

#[rustfmt::skip]
fn main() {
// Test cast_unnecessary
Expand Down Expand Up @@ -105,6 +115,13 @@ fn main() {
extern_fake_libc::getpid_SAFE_TRUTH() as i32;
let pid = unsafe { fake_libc::getpid() };
pid as i32;
aaa();
let x = aaa();
aaa();
// Will not lint currently.
bbb() as u32;
let x = bbb();
bbb() as u32;

let i8_ptr: *const i8 = &1;
let u8_ptr: *const u8 = &1;
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/unnecessary_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ mod fake_libc {
}
}

fn aaa() -> ::std::primitive::u32 {
0
}

use std::primitive::u32 as UnsignedThirtyTwoBitInteger;

fn bbb() -> UnsignedThirtyTwoBitInteger {
0
}

#[rustfmt::skip]
fn main() {
// Test cast_unnecessary
Expand Down Expand Up @@ -105,6 +115,13 @@ fn main() {
extern_fake_libc::getpid_SAFE_TRUTH() as i32;
let pid = unsafe { fake_libc::getpid() };
pid as i32;
aaa() as u32;
let x = aaa();
aaa() as u32;
// Will not lint currently.
bbb() as u32;
let x = bbb();
bbb() as u32;

let i8_ptr: *const i8 = &1;
let u8_ptr: *const u8 = &1;
Expand Down
Loading