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

CalculationsShouldNotOverflow.SyntaxKindWalker reduce allocations and evaluations in the hot path #8133

Merged

Conversation

martin-strecker-sonarsource
Copy link
Contributor

Fixes #8103

Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource left a comment

Choose a reason for hiding this comment

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

Minor polishing

}

[DataTestMethod]
[DataRow("_ = 1 + 1;", true)] // Fix: should be false as it is checked at compile time
Copy link
Contributor

Choose a reason for hiding this comment

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

What does the "Fix" mean here? I don't get it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are all cases with a binary operation but never an overflow. This is the case for constants (checked by the compiler) and string concatenation. The idea was to document the cases here and add an issue to fix later. But I wanted to know if there was enough value in adding it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unless we have FPs reported, I would not add issue for that.
Let's remove the Fix: prefix, the Should .... is enough to explain the problem

@martin-strecker-sonarsource
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM

""")).Tree;
var sut = new CalculationsShouldNotOverflow.SyntaxKindWalker();
sut.SafeVisit(tree.GetRoot());
sut.HasOverflow.Should().Be(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

There're dedicated methods for true and false. Same below.

Suggested change
sut.HasOverflow.Should().Be(false);
sut.HasOverflow.Should().BeFalse();

[DataRow("""_ = "" + s + s;""", true)] // Fix: should be false as one of the operands is a string literal
[DataRow("""_ = s + "" + s;""", true)] // Fix: should be false as one of the operands is a string literal
[DataRow("""_ = s + s + "";""", true)] // Fix: should be false as one of the operands is a string literal
public void HasOverflowExpressions_Literals(string statement, bool expected)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would remove the bool expected, as it's always true. If False is needed, second method can be added, or parameter can be re-added later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This misses a bit the purpose of the tests: Enable an incremental optimization by turning these into false. I kept it this way.

@sonarcloud
Copy link

sonarcloud bot commented Oct 17, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarcloud
Copy link

sonarcloud bot commented Oct 17, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@martin-strecker-sonarsource martin-strecker-sonarsource changed the title CalculationsShouldNotOverflow.SyntaxKindWalker contains contradicting conditions CalculationsShouldNotOverflow.SyntaxKindWalker reduce allocations and evaluations in the hot path Oct 19, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource merged commit 85f4909 into master Oct 19, 2023
22 checks passed
@martin-strecker-sonarsource martin-strecker-sonarsource deleted the Martin/ShouldNotOverlflow_Refactoring branch October 19, 2023 09:36
@martin-strecker-sonarsource
Copy link
Contributor Author

Peach validation: We lost a couple of issues that have an unchecked expression, that wasn't detected before.

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

Successfully merging this pull request may close these issues.

CalculationsShouldNotOverflow.SyntaxKindWalker reduce allocations and evaluations in the hot path
2 participants