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 NRE in recently added AssigningSymbolAndItsMemberInSameStatement … #2883

Merged
merged 1 commit into from Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -50,29 +50,21 @@ private void AnalyzeAssignment(OperationAnalysisContext context)
}

// This analyzer makes sense only for reference type objects
if (operationTarget.Instance?.Type.IsValueType == true)
if (operationTarget.Instance?.Type?.IsReferenceType != true)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the core fix for the NRE. Other change below is just applying an IDE suggestion to use switch expression.

{
return;
}

// Search for object equal to operationTarget.Instance further in assignment chain
bool isViolationFound = false;
if (operationTarget.Instance is ILocalReferenceOperation localInstance)
bool isViolationFound = operationTarget.Instance switch
{
isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, localInstance, (a, b) => a.Local.Equals(b.Local));
}
else if (operationTarget.Instance is IMemberReferenceOperation memberInstance)
{
isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, memberInstance, (a, b) => a.Member.Equals(b.Member) && a.Instance?.Syntax.ToString() == b.Instance?.Syntax.ToString());
}
else if (operationTarget.Instance is IParameterReferenceOperation parameterInstance)
{
isViolationFound = AnalyzeAssignmentToMember(assignmentOperation, parameterInstance, (a, b) => a.Parameter.Equals(b.Parameter));
}
else
{
return;
}
ILocalReferenceOperation localInstance =>
AnalyzeAssignmentToMember(assignmentOperation, localInstance, (a, b) => a.Local.Equals(b.Local)),
IMemberReferenceOperation memberInstance =>
AnalyzeAssignmentToMember(assignmentOperation, memberInstance, (a, b) => a.Member.Equals(b.Member) && a.Instance?.Syntax.ToString() == b.Instance?.Syntax.ToString()),
IParameterReferenceOperation parameterInstance =>
AnalyzeAssignmentToMember(assignmentOperation, parameterInstance, (a, b) => a.Parameter.Equals(b.Parameter)),
_ => false,
};

if (isViolationFound)
{
Expand Down
Expand Up @@ -316,5 +316,20 @@ public void Method()
}
", TestValidationMode.AllowCompileErrors);
}

[Fact]
public void CSharpAssignmentInCodeWithOperationNone()
{
VerifyCSharpUnsafeCode(@"
public struct Test
{
public System.IntPtr PtrField;
public unsafe void Method(Test a, Test *b)
{
b->PtrField = a.PtrField;
}
}
");
}
}
}