-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add needless_type_cast lint
#16139
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
Open
borngraced
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
borngraced:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+760
−2
Open
Add needless_type_cast lint
#16139
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ mod fn_to_numeric_cast; | |
| mod fn_to_numeric_cast_any; | ||
| mod fn_to_numeric_cast_with_truncation; | ||
| mod manual_dangling_ptr; | ||
| mod needless_type_cast; | ||
| mod ptr_as_ptr; | ||
| mod ptr_cast_constness; | ||
| mod ref_as_ptr; | ||
|
|
@@ -813,6 +814,32 @@ declare_clippy_lint! { | |
| "casting a primitive method pointer to any integer type" | ||
| } | ||
|
|
||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Checks for bindings (constants, statics, or let bindings) that are defined | ||
| /// with one numeric type but are consistently cast to a different type in all usages. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// If a binding is always cast to a different type when used, it would be clearer | ||
| /// and more efficient to define it with the target type from the start. | ||
| /// | ||
| /// ### Example | ||
| /// ```no_run | ||
| /// const SIZE: u16 = 15; | ||
| /// let arr: [u8; SIZE as usize] = [0; SIZE as usize]; | ||
| /// ``` | ||
| /// | ||
| /// Use instead: | ||
| /// ```no_run | ||
| /// const SIZE: usize = 15; | ||
| /// let arr: [u8; SIZE] = [0; SIZE]; | ||
| /// ``` | ||
| #[clippy::version = "1.93.0"] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we manage to merge before Thursday, this is ok. Otherwise we'll have to increment. |
||
| pub NEEDLESS_TYPE_CAST, | ||
| pedantic, | ||
| "binding defined with one type but always cast to another" | ||
| } | ||
|
|
||
| pub struct Casts { | ||
| msrv: Msrv, | ||
| } | ||
|
|
@@ -851,6 +878,7 @@ impl_lint_pass!(Casts => [ | |
| AS_POINTER_UNDERSCORE, | ||
| MANUAL_DANGLING_PTR, | ||
| CONFUSING_METHOD_TO_NUMERIC_CAST, | ||
| NEEDLESS_TYPE_CAST, | ||
| ]); | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for Casts { | ||
|
|
@@ -920,4 +948,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts { | |
| cast_slice_different_sizes::check(cx, expr, self.msrv); | ||
| ptr_cast_constness::check_null_ptr_cast_method(cx, expr); | ||
| } | ||
|
|
||
| fn check_body(&mut self, cx: &LateContext<'tcx>, body: &rustc_hir::Body<'tcx>) { | ||
| needless_type_cast::check(cx, body); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,289 @@ | ||
| use clippy_utils::diagnostics::span_lint_and_sugg; | ||
| use clippy_utils::visitors::{Descend, for_each_expr, for_each_expr_without_closures}; | ||
| use core::ops::ControlFlow; | ||
| use rustc_data_structures::fx::FxHashMap; | ||
| use rustc_errors::Applicability; | ||
| use rustc_hir::def::{DefKind, Res}; | ||
| use rustc_hir::{BlockCheckMode, Body, Expr, ExprKind, HirId, LetStmt, PatKind, StmtKind, UnsafeSource}; | ||
| use rustc_lint::LateContext; | ||
| use rustc_middle::ty::{Ty, TypeVisitableExt}; | ||
| use rustc_span::Span; | ||
|
|
||
| use super::NEEDLESS_TYPE_CAST; | ||
|
|
||
| struct BindingInfo<'a> { | ||
| source_ty: Ty<'a>, | ||
| ty_span: Span, | ||
| } | ||
|
|
||
| struct UsageInfo<'a> { | ||
| cast_to: Option<Ty<'a>>, | ||
| in_generic_context: bool, | ||
| } | ||
|
|
||
| pub(super) fn check<'a>(cx: &LateContext<'a>, body: &Body<'a>) { | ||
| let mut bindings: FxHashMap<HirId, BindingInfo<'a>> = FxHashMap::default(); | ||
|
|
||
| for_each_expr_without_closures(body.value, |expr| { | ||
| match expr.kind { | ||
| ExprKind::Block(block, _) => { | ||
| for stmt in block.stmts { | ||
| if let StmtKind::Let(let_stmt) = stmt.kind { | ||
| collect_binding_from_local(cx, let_stmt, &mut bindings); | ||
| } | ||
| } | ||
| }, | ||
| ExprKind::Let(let_expr) => { | ||
| collect_binding_from_let(cx, let_expr, &mut bindings); | ||
| }, | ||
| _ => {}, | ||
| } | ||
| ControlFlow::<()>::Continue(()) | ||
| }); | ||
|
|
||
| #[allow(rustc::potential_query_instability)] | ||
| let mut binding_vec: Vec<_> = bindings.into_iter().collect(); | ||
| binding_vec.sort_by_key(|(_, info)| info.ty_span.lo()); | ||
|
|
||
| for (hir_id, binding_info) in binding_vec { | ||
| check_binding_usages(cx, body, hir_id, &binding_info); | ||
| } | ||
| } | ||
|
|
||
| fn collect_binding_from_let<'a>( | ||
| cx: &LateContext<'a>, | ||
| let_expr: &rustc_hir::LetExpr<'a>, | ||
| bindings: &mut FxHashMap<HirId, BindingInfo<'a>>, | ||
| ) { | ||
| if let_expr.ty.is_none() | ||
| || let_expr.span.from_expansion() | ||
| || has_generic_return_type(cx, let_expr.init) | ||
| || contains_unsafe(let_expr.init) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if let PatKind::Binding(_, hir_id, _, _) = let_expr.pat.kind | ||
| && let Some(ty_hir) = let_expr.ty | ||
| { | ||
| let ty = cx.typeck_results().pat_ty(let_expr.pat); | ||
| if ty.is_numeric() { | ||
| bindings.insert( | ||
| hir_id, | ||
| BindingInfo { | ||
| source_ty: ty, | ||
| ty_span: ty_hir.span, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn collect_binding_from_local<'a>( | ||
| cx: &LateContext<'a>, | ||
| let_stmt: &LetStmt<'a>, | ||
| bindings: &mut FxHashMap<HirId, BindingInfo<'a>>, | ||
| ) { | ||
| if let_stmt.ty.is_none() | ||
| || let_stmt.span.from_expansion() | ||
| || let_stmt | ||
| .init | ||
| .is_some_and(|init| has_generic_return_type(cx, init) || contains_unsafe(init)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| if let PatKind::Binding(_, hir_id, _, _) = let_stmt.pat.kind | ||
| && let Some(ty_hir) = let_stmt.ty | ||
| { | ||
| let ty = cx.typeck_results().pat_ty(let_stmt.pat); | ||
| if ty.is_numeric() { | ||
| bindings.insert( | ||
| hir_id, | ||
| BindingInfo { | ||
| source_ty: ty, | ||
| ty_span: ty_hir.span, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn contains_unsafe(expr: &Expr<'_>) -> bool { | ||
| for_each_expr_without_closures(expr, |e| { | ||
| if let ExprKind::Block(block, _) = e.kind | ||
| && let BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) = block.rules | ||
| { | ||
| return ControlFlow::Break(()); | ||
| } | ||
| ControlFlow::Continue(()) | ||
| }) | ||
| .is_some() | ||
| } | ||
|
|
||
| fn has_generic_return_type(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { | ||
| match &expr.kind { | ||
| ExprKind::Block(block, _) => { | ||
| if let Some(tail_expr) = block.expr { | ||
| return has_generic_return_type(cx, tail_expr); | ||
| } | ||
| false | ||
| }, | ||
| ExprKind::If(_, then_block, else_expr) => { | ||
| has_generic_return_type(cx, then_block) || else_expr.is_some_and(|e| has_generic_return_type(cx, e)) | ||
| }, | ||
| ExprKind::Match(_, arms, _) => arms.iter().any(|arm| has_generic_return_type(cx, arm.body)), | ||
| ExprKind::Loop(block, label, ..) => for_each_expr_without_closures(*block, |e| { | ||
| match e.kind { | ||
| ExprKind::Loop(..) => { | ||
| // Unlabeled breaks inside nested loops target the inner loop, not ours | ||
| return ControlFlow::Continue(Descend::No); | ||
| }, | ||
| ExprKind::Break(dest, Some(break_expr)) => { | ||
| let targets_this_loop = | ||
| dest.label.is_none() || dest.label.map(|l| l.ident) == label.map(|l| l.ident); | ||
| if targets_this_loop && has_generic_return_type(cx, break_expr) { | ||
| return ControlFlow::Break(()); | ||
| } | ||
| }, | ||
| _ => {}, | ||
| } | ||
| ControlFlow::Continue(Descend::Yes) | ||
| }) | ||
| .is_some(), | ||
| ExprKind::MethodCall(..) => { | ||
| if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) { | ||
| let sig = cx.tcx.fn_sig(def_id).instantiate_identity(); | ||
| let ret_ty = sig.output().skip_binder(); | ||
| return ret_ty.has_param(); | ||
| } | ||
| false | ||
| }, | ||
| ExprKind::Call(callee, _) => { | ||
| if let ExprKind::Path(qpath) = &callee.kind { | ||
| let res = cx.qpath_res(qpath, callee.hir_id); | ||
| if let Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) = res { | ||
| let sig = cx.tcx.fn_sig(def_id).instantiate_identity(); | ||
| let ret_ty = sig.output().skip_binder(); | ||
| return ret_ty.has_param(); | ||
| } | ||
| } | ||
| false | ||
| }, | ||
| _ => false, | ||
| } | ||
| } | ||
|
|
||
| fn is_generic_res(cx: &LateContext<'_>, res: Res) -> bool { | ||
| let has_type_params = |def_id| { | ||
| cx.tcx | ||
| .generics_of(def_id) | ||
| .own_params | ||
| .iter() | ||
| .any(|p| p.kind.is_ty_or_const()) | ||
| }; | ||
| match res { | ||
| Res::Def(DefKind::Fn | DefKind::AssocFn, def_id) => has_type_params(def_id), | ||
| // Ctor → Variant → ADT: constructor's parent is variant, variant's parent is the ADT | ||
| Res::Def(DefKind::Ctor(..), def_id) => has_type_params(cx.tcx.parent(cx.tcx.parent(def_id))), | ||
| _ => false, | ||
| } | ||
| } | ||
|
|
||
| fn is_cast_in_generic_context<'a>(cx: &LateContext<'a>, cast_expr: &Expr<'a>) -> bool { | ||
| let mut current_id = cast_expr.hir_id; | ||
|
|
||
| loop { | ||
| let parent_id = cx.tcx.parent_hir_id(current_id); | ||
| if parent_id == current_id { | ||
| return false; | ||
| } | ||
|
|
||
| let parent = cx.tcx.hir_node(parent_id); | ||
|
|
||
| match parent { | ||
| rustc_hir::Node::Expr(parent_expr) => { | ||
| match &parent_expr.kind { | ||
| ExprKind::Closure(_) => return false, | ||
| ExprKind::Call(callee, _) => { | ||
| if let ExprKind::Path(qpath) = &callee.kind { | ||
| let res = cx.qpath_res(qpath, callee.hir_id); | ||
| if is_generic_res(cx, res) { | ||
| return true; | ||
| } | ||
| } | ||
| }, | ||
| ExprKind::MethodCall(..) => { | ||
| if let Some(def_id) = cx.typeck_results().type_dependent_def_id(parent_expr.hir_id) | ||
| && cx | ||
| .tcx | ||
| .generics_of(def_id) | ||
| .own_params | ||
| .iter() | ||
| .any(|p| p.kind.is_ty_or_const()) | ||
| { | ||
| return true; | ||
| } | ||
| }, | ||
| _ => {}, | ||
| } | ||
| current_id = parent_id; | ||
| }, | ||
| _ => return false, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn check_binding_usages<'a>(cx: &LateContext<'a>, body: &Body<'a>, hir_id: HirId, binding_info: &BindingInfo<'a>) { | ||
| let mut usages = Vec::new(); | ||
|
|
||
| for_each_expr(cx, body.value, |expr| { | ||
| if let ExprKind::Path(ref qpath) = expr.kind | ||
| && !expr.span.from_expansion() | ||
| && let Res::Local(id) = cx.qpath_res(qpath, expr.hir_id) | ||
| && id == hir_id | ||
| { | ||
| let parent_id = cx.tcx.parent_hir_id(expr.hir_id); | ||
| let parent = cx.tcx.hir_node(parent_id); | ||
|
|
||
| let usage = if let rustc_hir::Node::Expr(parent_expr) = parent | ||
| && let ExprKind::Cast(..) = parent_expr.kind | ||
| && !parent_expr.span.from_expansion() | ||
| { | ||
| UsageInfo { | ||
| cast_to: Some(cx.typeck_results().expr_ty(parent_expr)), | ||
| in_generic_context: is_cast_in_generic_context(cx, parent_expr), | ||
| } | ||
| } else { | ||
| UsageInfo { | ||
| cast_to: None, | ||
| in_generic_context: false, | ||
| } | ||
| }; | ||
| usages.push(usage); | ||
| } | ||
| ControlFlow::<()>::Continue(()) | ||
| }); | ||
|
|
||
| let Some(first_target) = usages | ||
| .first() | ||
| .and_then(|u| u.cast_to) | ||
| .filter(|&t| t != binding_info.source_ty) | ||
| .filter(|&t| usages.iter().all(|u| u.cast_to == Some(t) && !u.in_generic_context)) | ||
| else { | ||
| return; | ||
| }; | ||
|
|
||
| span_lint_and_sugg( | ||
| cx, | ||
| NEEDLESS_TYPE_CAST, | ||
| binding_info.ty_span, | ||
| format!( | ||
| "this binding is defined as `{}` but is always cast to `{}`", | ||
| binding_info.source_ty, first_target | ||
| ), | ||
| "consider defining it as", | ||
| first_target.to_string(), | ||
| Applicability::MaybeIncorrect, | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoa!