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

Reproducer for S1905 FP: Floating point arithmetic #9520

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,14 @@ public void Foo(IEnumerable enumerable, IEnumerable<object> genericEnumerable)

public void OfType() => OfType();
}

// https://github.com/SonarSource/sonar-dotnet/issues/9498
public class Repro_9498
{
void Sample()
{
float x = 3.14f;
float res = 1 / (float)(x * 2); // Fixed
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not fix casts, only .Cast<T> and as T expressions.

}
}
}
10 changes: 10 additions & 0 deletions analyzers/tests/SonarAnalyzer.Test/TestCases/RedundantCast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,14 @@ public void Foo(IEnumerable enumerable, IEnumerable<object> genericEnumerable)

public void OfType() => OfType();
}

// https://github.com/SonarSource/sonar-dotnet/issues/9498
public class Repro_9498
{
void Sample()
{
float x = 3.14f;
float res = 1 / (float)(x * 2); // Noncompliant FP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you find an example where the precision change is visible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it matters, as the solution would probably be what Jared suggested on the post.
Which is to be conservative and raise only in very few circumstances, when the RHS is a variable.

Even more conservative, we could also not raise on floating point arithmetic at all.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should document a case where this is actually an FP and think about a concrete solution later (or consider it a niche case and ignore it).

}
}
}
Loading