-
Notifications
You must be signed in to change notification settings - Fork 14k
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
Open
GuillaumeGomez
wants to merge
10
commits into
rust-lang:master
Choose a base branch
from
GuillaumeGomez:function_casts_as_integer
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.
+307
−119
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
56f3007
Add new `function_casts_as_integer` lint
GuillaumeGomez edcae85
Fix new `function_casts_as_integer` lint errors in core, std, panic_u…
GuillaumeGomez ee18e3f
Allow `function_casts_as_integer` in non-related ui tests
GuillaumeGomez 44efbde
Add ui test for `function_casts_as_integer` lint
GuillaumeGomez 64434a3
Allow `function_casts_as_integer` in non-related clippy ui tests
GuillaumeGomez c9f7b95
Allow `function_casts_as_integer` in non-related miri tests
GuillaumeGomez d21be6a
Allow `function_casts_as_integer` in coretest test
GuillaumeGomez 488f720
Allow `function_casts_as_integer` in miri source code
GuillaumeGomez 5038877
Convert `function_cast_as_integer` lint suggestion to plain `*const (…
GuillaumeGomez 76b37b2
Fix typos
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,60 @@ | ||
| 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. | ||
| 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.
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.
It is probably better to check that the type which is being cast to is actually a integer, instead of checking if it's not something else.