-
Notifications
You must be signed in to change notification settings - Fork 124
Detect redundant cast usage and emit warning #634
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
base: main
Are you sure you want to change the base?
Conversation
Hi @sanma613! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
if let Some(val_expr) = val { | ||
let val_type = self.expr_infer(val_expr, errors); | ||
|
||
if self.subtype(&val_type, &ret) { |
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 don't think subtype
is a function?
Try is_subset_eq
range, | ||
ErrorKind::RedundantCast, | ||
None, | ||
"`typing.cast` is unnecessary because the value already has the target type".to_owned(), |
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 message is slightly misleading if the thing being cast is Any
why not something simpler like "redundant cast to {}
"? note that when we pass the type we should call .deterministic_printing()
on it
r#" | ||
from typing import cast | ||
|
||
x = cast(int, 5) # E: Redundant cast: expression already has type `int` |
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.
these messages don't seem to match the implementaiton in special_calls.rs
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.
In addition to other comments, don't forget to run cargo fmt and cargo clippy
Thanks!
Summary
This PR adds detection for redundant usage of
typing.cast
where the value being cast already matches the target type. In such cases, a warning is now emitted to inform users of unnecessary casting.Implementation Details
ErrorKind::RedundantCast
warning is emitted instead of applying the cast.redundant_cast.rs
.Example
Testing
test_redundant_cast_simple
test_redundant_cast_with_annotation
test_non_redundant_cast
Closes #593