-
Notifications
You must be signed in to change notification settings - Fork 231
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
Fix S3900 FN: Conversion operations #7002
Conversation
There was a problem hiding this 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.
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()))); |
There was a problem hiding this comment.
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.
There was a problem hiding this 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()))); |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
?
{ | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this 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
public void ForEachLoop(object[] array) | ||
{ | ||
foreach (object o in array) { } // Noncompliant | ||
} |
There was a problem hiding this comment.
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.
public void CastWithPropertyAccess(object o) | ||
{ | ||
_ = ((Exception)o).Message; // Noncompliant | ||
} |
There was a problem hiding this comment.
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
public void CastWithPropertyAccess(object o) | ||
{ | ||
_ = ((Exception)o).Message; // Noncompliant | ||
} |
There was a problem hiding this comment.
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;
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
Part of #6997
Task 1 and 9