-
Notifications
You must be signed in to change notification settings - Fork 1.6k
WIP: Add lint on cast Fn to all numerical except usize. #2814
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
Conversation
Thanks for the contribution!
Since the better cast would be to Bonus points if you manage to use the
Imo this is unnecessary. |
Thank you for review. So, I have changed lint type to unique one, fix message and add suggestion.
is better way than
Real type is more explicit. Or in such place it's excessive? |
good catch, that would indeed be better. You can set the function type and the cast type in ticks: ` |
clippy_lints/src/types.rs
Outdated
expr.span, | ||
&format!("casting a Fn to {} may truncate the function address value.", cast_to), | ||
"if you need address of function, use cast to `usize` instead:", | ||
format!("{} as usize", &snippet(cx, ex.span, "x")) |
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.
This looks good! I think for the suggestion message "if you need the address of the function, consider:"
would be enough, since the usize
hint is already in the suggestion.
clippy_lints/src/types.rs
Outdated
@@ -679,6 +679,23 @@ declare_clippy_lint! { | |||
"cast to the same type, e.g. `x as i32` where `x: i32`" | |||
} | |||
|
|||
/// **What it does:** Checks for casts function pointer to the numeric type. | |||
/// | |||
/// **Why is this bad?** Cast pointer not to usize truncate value. |
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.
What it does: "Checks for casts of a function pointer to a numeric type except `usize`."
Why is this bad? Casting a function pointer to something other than `usize` could truncate the address value.
This would be a little more precise, what this lint does.
Oh and could you add another test for a function which is not called |
I've done it. But I'm confused a bit. Why format of help strongly depends on number of words in 'help' message?
it differs only by one whitespace before ':' and dramatically change whole view. I think that first one is better because with such big 'help' message second one looks not so good. Is there a way to set format explicitly? Or which way is better here? |
That is exactly why it depends on the number of signs/words in the help message.
Not really. What you could do is change the help message once more: just Sorry for the back and forth with the help/lint messages. |
No problem! This help-message auto-formatting logic looks a bit weird at first time. Hope now it's OK. |
LGTM. Thanks for all the work! |
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.
Just a test-nit, lgtm otherwise
tests/ui/types_fn_to_int.rs
Outdated
|
||
fn main() { | ||
let x = Foo::A; | ||
let y = x as i32; |
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.
Can you also add a check for Foo::A as i32
directly without an intermediate variable?
Hmm. I remembered about 128 bit types.
it looks not correct enough. Should I fix error message or add constraints for this lint (allow cast to 'usize' or integer type with bigger size)? |
Hmm. I personally want all casts to go through a usize cast, so maybe just improve the error message for u64 and u128? |
FN_TO_NUMERIC_CAST_WITH_TRUNCATION is correctness check FN_TO_NUMERIC_CAST is only style check
I have thought about this. I think that conversion without truncation and with one is not the same, so I decide to divide this lint to two ones. One of them is about 'correctness' and other is about 'style'. What do you think on it? |
Excuse me, @oli-obk, please give me feedback. |
Sorry this got lost in my emails. Yes I agree with your conclusion to make two lints out of it and with the categories you placed it into. |
…ation This removes the code added in #2814
This is should fix #2588. But I'm novice here and not sure is it OK.
First of all - should I add some new lint type or can use UNNECESSARY_CAST for example? Is this lint message correct?
Also I have been trying to add more specific message for casting Enum constructor to int but I can't find a way to check is 'cast_from' function returns Enum variant. May be it's unnecessary?
Thanks for review.