Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

sanma613
Copy link

@sanma613 sanma613 commented Jul 6, 2025

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

  • A check was added before applying the cast to compare the inferred type of the value against the target type.
  • If both types match, an ErrorKind::RedundantCast warning is emitted instead of applying the cast.
  • The cast still proceeds (non-fatal) to maintain soundness.
  • Includes tests under redundant_cast.rs.

Example

from typing import cast

def f(x: int) -> int:
    return cast(int, x)  # warning: Redundant cast

Testing

  • Added test case: test_redundant_cast_simple
  • Added test case: test_redundant_cast_with_annotation
  • Added test case: test_non_redundant_cast

Closes #593

@facebook-github-bot
Copy link
Contributor

Hi @sanma613!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@facebook-github-bot
Copy link
Contributor

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) {
Copy link
Contributor

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(),
Copy link
Contributor

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`
Copy link
Contributor

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

Copy link
Contributor

@yangdanny97 yangdanny97 left a 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] warn on redundant cast
3 participants