-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
Add new function_casts_as_integer lint
#141470
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
bors
merged 12 commits into
rust-lang:main
from
GuillaumeGomez:function_casts_as_integer
Nov 11, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a23b8c4
Add new `function_casts_as_integer` lint
GuillaumeGomez 40be1db
Fix new `function_casts_as_integer` lint errors in core, std, panic_u…
GuillaumeGomez 725f4ad
Allow `function_casts_as_integer` in non-related ui tests
GuillaumeGomez 2ac32ae
Add ui test for `function_casts_as_integer` lint
GuillaumeGomez 37eb906
Allow `function_casts_as_integer` in non-related clippy ui tests
GuillaumeGomez 065d41e
Allow `function_casts_as_integer` in non-related miri tests
GuillaumeGomez 6e14bea
Allow `function_casts_as_integer` in coretest test
GuillaumeGomez a99d100
Allow `function_casts_as_integer` in miri source code
GuillaumeGomez 0a2c473
Convert `function_cast_as_integer` lint suggestion to plain `*const (…
GuillaumeGomez f22600e
Fix typos
GuillaumeGomez d05f297
Add more test for `function_cast_as_integer` lint
GuillaumeGomez 66b4e93
Add instructions on what to do in case casts other than to integers i…
GuillaumeGomez 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| use rustc_hir as hir; | ||
| use rustc_middle::ty; | ||
| use rustc_session::{declare_lint, declare_lint_pass}; | ||
| use rustc_span::BytePos; | ||
|
|
||
| use crate::lints::{FunctionCastsAsIntegerDiag, FunctionCastsAsIntegerSugg}; | ||
| use crate::{LateContext, LateLintPass}; | ||
|
|
||
| declare_lint! { | ||
| /// The `function_casts_as_integer` lint detects cases where a function item is cast | ||
| /// to an integer. | ||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// ```rust | ||
| /// fn foo() {} | ||
| /// let x = foo as usize; | ||
| /// ``` | ||
| /// | ||
| /// {{produces}} | ||
| /// | ||
| /// ### Explanation | ||
| /// | ||
| /// When casting a function item to an integer, it implicitly creates a | ||
| /// function pointer that will in turn be cast to an integer. By making | ||
| /// it explicit, it improves readability of the code and prevents bugs. | ||
| pub FUNCTION_CASTS_AS_INTEGER, | ||
| Warn, | ||
| "casting a function into an integer", | ||
| } | ||
|
|
||
| declare_lint_pass!( | ||
| /// Lint for casts of functions into integers. | ||
| FunctionCastsAsInteger => [FUNCTION_CASTS_AS_INTEGER] | ||
| ); | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for FunctionCastsAsInteger { | ||
| fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { | ||
| let hir::ExprKind::Cast(cast_from_expr, cast_to_expr) = expr.kind else { return }; | ||
| let cast_to_ty = cx.typeck_results().expr_ty(expr); | ||
| // Casting to a function (pointer?), so all good. | ||
| // | ||
| // Normally, only casts to integers is possible, but if it ever changed, this condition | ||
| // will likely need to be updated. | ||
| if matches!(cast_to_ty.kind(), ty::FnDef(..) | ty::FnPtr(..) | ty::RawPtr(..)) { | ||
| return; | ||
| } | ||
| let cast_from_ty = cx.typeck_results().expr_ty(cast_from_expr); | ||
| if matches!(cast_from_ty.kind(), ty::FnDef(..)) { | ||
| cx.tcx.emit_node_span_lint( | ||
| FUNCTION_CASTS_AS_INTEGER, | ||
| expr.hir_id, | ||
| cast_to_expr.span.with_lo(cast_from_expr.span.hi() + BytePos(1)), | ||
| FunctionCastsAsIntegerDiag { | ||
| sugg: FunctionCastsAsIntegerSugg { | ||
| suggestion: cast_from_expr.span.shrink_to_hi(), | ||
| cast_to_ty, | ||
| }, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
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
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
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
1 change: 1 addition & 0 deletions
1
src/tools/clippy/tests/ui/confusing_method_to_numeric_cast.fixed
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.