Skip to content
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

Do not generate type-indecisive expressions in unnecessary_cast #14492

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

samueltardieu
Copy link
Contributor

This change requires fixing the type certainty evaluation:

  • unsuffixed integral or floating-point literals are considered of unknown type unless their type is constrained or they are used as a function or method argument
  • if the type of one argument in a binary expression is certain and the other is not, consider the type of the binary expression as being known as well (without more precisions)

This might be to be refined further, but this does not break any existing test, and allows for new fixes such as this one.

Fix #14366

changelog: [unnecessary_cast]: do not remove casts if the expression would become type-indecisive

However, if they appear within the context of a function argument, they
will be marked as certain, as they might eventually be considered `i32`
by default if the context is not more specific.

Also, in the case of binary expressions, if one of the side is
uncertain, then the certainty will come from the other side.
@rustbot
Copy link
Collaborator

rustbot commented Mar 28, 2025

r? @Alexendoo

rustbot has assigned @Alexendoo.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 28, 2025
@samueltardieu
Copy link
Contributor Author

samueltardieu commented Mar 28, 2025

The lintcheck run contains new false negatives though.

@samueltardieu samueltardieu marked this pull request as draft March 29, 2025 00:29
@samueltardieu samueltardieu force-pushed the push-omoqnppxvzpv branch 2 times, most recently from 7f627a4 to 95968ac Compare March 30, 2025 20:56
Associated constants of primitive types are resolved as `Res::Err` in
the HIR. To force a resolution, the primitive type must be recognized as
certain. Since they are not ADT, they don't have a `DefId`. By allowing
the `Certainty` type to embed a `TypeKind` which is either a `PrimTy` or
a `DefId`, we allow primitives types to be resolved, and the associated
constants as well.

Also, in method calls, if the receiver is not certain, consider that the
overall call is not either.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unnecessary_cast fix suggestion introduces compile error for (expr_with_float_literals as f64).powf(2.0)
3 participants