-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
7
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.
+312
−124
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
48f464b
Add new `function_casts_as_integer` lint
GuillaumeGomez 955accf
Fix new `function_casts_as_integer` lint errors in core, std, panic_u…
GuillaumeGomez 96db7c6
Allow `function_casts_as_integer` in non-related ui tests
GuillaumeGomez 56746aa
Add ui test for `function_casts_as_integer` lint
GuillaumeGomez 7923b85
Allow `function_casts_as_integer` in non-related clippy ui tests
GuillaumeGomez 0f4189c
Allow `function_casts_as_integer` in non-related miri tests
GuillaumeGomez f123b28
Allow `function_casts_as_integer` in coretest test
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
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,62 @@ | ||
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 casted | ||
/// into an integer. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust | ||
/// fn foo() {} | ||
/// let x = foo as usize; | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// When casting a function item into an integer, it's implicitly creating a | ||
/// function pointer that will in turn be casted into 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(..)) { | ||
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(), | ||
// We get the function pointer to have a nice display. | ||
cast_from_ty: cx.typeck_results().expr_ty_adjusted(cast_from_expr), | ||
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
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
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.
Clippy has a few lints for
fn
to integer casts. But they are all restriction or style lints in Clippy. Adding a warn-by-default lint about this to rustc might be a bit aggressive 🤔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.
I know, I implemented one myself. 😉 I think it highlights the fact that this is a big issue and that the compiler should warn about it and eventually even forbid this fn to integer cast (you need to cast to an fn pointer first).
But in any case, it's up to the lang team.
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.
Agreed 👍 Just want to add this information as "prior art" for the lang team to make this decision. Even though it might've sounded like it, I'm not against adding this lint to rustc.
Clippy question: Do you think if this lint gets added to rustc, we can (partially) deprecate Clippy lints?
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.
Hard to say. For example
confusing_method_to_numeric_cast
provides extra information about what (likely) went wrong. But with the current lint, they likely would already have seen the problem and fixed it. So by default I'd say yes. But we could eventually uplift part of them to add the extra context clippy has that this lint doesn't provide. Would make it much more interesting and even more useful.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.
Yeah, a partial uplift might be good then, should this be accepted.