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

Fix S3900 FN: Conversion operations #7002

Merged
merged 10 commits into from
Mar 29, 2023

Conversation

zsolt-kolbay-sonarsource
Copy link
Contributor

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource commented Mar 29, 2023

Part of #6997
Task 1 and 9

Copy link
Contributor

@Tim-Pohlmann Tim-Pohlmann left a comment

Choose a reason for hiding this comment

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

LGTM, only a minor formatting nitpick.

Comment on lines 94 to 103
private static bool IsParameterDereferenced(IOperationWrapperSonar operation) =>
operation.Parent != null
&& operation.Parent.IsAnyKind(
&& (operation.Parent.IsAnyKind(
OperationKindEx.Invocation,
OperationKindEx.FieldReference,
OperationKindEx.PropertyReference,
OperationKindEx.EventReference,
OperationKindEx.Await,
OperationKindEx.ArrayElementReference);
OperationKindEx.ArrayElementReference)
|| (operation.Parent.Kind == OperationKindEx.Conversion && IsParameterDereferenced(operation.Parent.ToSonar())));
Copy link
Contributor

Choose a reason for hiding this comment

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

The formatting makes this hard to understand. Try indenting the last line.

Copy link
Contributor

@antonioaversa antonioaversa left a comment

Choose a reason for hiding this comment

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

LGTM, just proposal to make the main logical condition more readable and to add a couple of interesting tests.

OperationKindEx.Invocation,
OperationKindEx.FieldReference,
OperationKindEx.PropertyReference,
OperationKindEx.EventReference,
OperationKindEx.Await,
OperationKindEx.ArrayElementReference);
OperationKindEx.ArrayElementReference)
|| (operation.Parent.Kind == OperationKindEx.Conversion && IsParameterDereferenced(operation.Parent.ToSonar())));
Copy link
Contributor

Choose a reason for hiding this comment

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

The || operator should be indented one level further w.r.t. the && operator above (to which is not aligned either, by one char).
I think however, that it would be better to simplify the structure of this logical condition, since you have different logical operators at different levels of nesting.

@@ -93,13 +93,14 @@ protected override ProgramState PreProcessSimple(SymbolicContext context)

private static bool IsParameterDereferenced(IOperationWrapperSonar operation) =>
operation.Parent != null
&& operation.Parent.IsAnyKind(
&& (operation.Parent.IsAnyKind(
Copy link
Contributor

Choose a reason for hiding this comment

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

This condition is getting a bit difficult to read.
Maybe it would make sense to build an array of OperationKindEx, to be used in IsAnyKind?

{
}
}
}
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 also add the following unit test, which was not working before the change from Parent to Ancestors(), and works now:

public void Parentheses(object[] arr)
{
    foreach (object o in ((arr))) { } // Noncompliant
}

If you want to push it one level further:

public void Parentheses(object[] arr)
{
    foreach (object o in ((object[])(object)((arr)))) { } // Noncompliant
}

and similar.

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, please add few more test cases before merging

Comment on lines +97 to +100
public void ForEachLoop(object[] array)
{
foreach (object o in array) { } // Noncompliant
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add also IEnumerable<object>. Can be 2nd parameter of the same method.

Comment on lines 649 to 652
public void CastWithPropertyAccess(object o)
{
_ = ((Exception)o).Message; // Noncompliant
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add also field access

Comment on lines 649 to 652
public void CastWithPropertyAccess(object o)
{
_ = ((Exception)o).Message; // Noncompliant
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd also add delegate access, should be FN for now - as seen in the original issue

_ = ((Exception)o).ToString;

@sonarqubecloud
Copy link

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

@sonarqubecloud
Copy link

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

@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource merged commit 94ce050 into feature/SE Mar 29, 2023
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource deleted the Zsolt/S3900-conversion-operator branch March 29, 2023 15:37
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.

4 participants