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

Inline temporary variable adds unnecessary cast #69869

Closed
DoctorKrolic opened this issue Sep 9, 2023 · 7 comments · Fixed by #70423
Closed

Inline temporary variable adds unnecessary cast #69869

DoctorKrolic opened this issue Sep 9, 2023 · 7 comments · Fixed by #70423
Labels
Area-IDE help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Milestone

Comments

@DoctorKrolic
Copy link
Contributor

Version Used:
Latest main

Steps to Reproduce:

var a = Math.Round(1.1D);
var b = a;

Inline variable a;

Expected Behavior:

var b = Math.Round(1.1D);

Actual Behavior:

var b = (double)Math.Round(1.1D);
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 9, 2023
@DoctorKrolic
Copy link
Contributor Author

Futhermore, after executing code action, IDE doesn't suggest you remove unnecessary cast. So this is simplifier's problem: it cannot determine, that in the given context overload with double return type is used (I guess this is the core problem)

@CyrusNajmabadi
Copy link
Member

Doubles are challenging due to the casts having semantics even when the types are exactly the same.

We likely want to be cautious about adding the explicit fp cast in the first place. Avoiding it if we can be sure it isn't necessary.

@CyrusNajmabadi CyrusNajmabadi added this to the Backlog milestone Sep 9, 2023
@CyrusNajmabadi CyrusNajmabadi added help wanted The issue is "up for grabs" - add a comment if you are interested in working on it and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Sep 9, 2023
@DoctorKrolic
Copy link
Contributor Author

Doubles are challenging due to the casts having semantics even when the types are exactly the same.

Wait what?! Can you give an example?

@CyrusNajmabadi
Copy link
Member

CyrusNajmabadi commented Sep 9, 2023

Machines often execute fp operations on higher precision than 32 or 64 bits. Casting explicitly reduces the precision down to those levels. So you can get different results.

@pyoub
Copy link
Contributor

pyoub commented Oct 18, 2023

i create a pr triying to fix this problem if you can review it please :)

@CyrusNajmabadi
Copy link
Member

@pyoub if you ping me tomorrow I can take a look!

@pyoub
Copy link
Contributor

pyoub commented Oct 19, 2023

@CyrusNajmabadi can you check the PR please @DoctorKrolic already did and didnt like approve the solution. so is there any suggestion?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
Status: Completed
Development

Successfully merging a pull request may close this issue.

3 participants