Skip to content
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.
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 @@ -84,15 +84,16 @@ public override void Initialize(AnalysisContext context)
IArgumentOperation destinationArgument = arguments[2];
IArgumentOperation countArgument = arguments[4];

bool CheckArrayLengthLocalReference(IArgumentOperation targetArgument, IPropertyReferenceOperation lengthPropertyArgument)
bool CheckArgumentArrayType(IArgumentOperation targetArgument, IPropertyReferenceOperation lengthPropertyArgument)
{
if (targetArgument.Value is IConversionOperation targetArgumentValue)
{
if (lengthPropertyArgument.Instance.GetReferencedMemberOrLocalOrParameter() == targetArgumentValue.Operand.GetReferencedMemberOrLocalOrParameter())
{
IArrayTypeSymbol countArgumentArrayTypeSymbol = (IArrayTypeSymbol)lengthPropertyArgument.Instance.Type;
if (countArgumentArrayTypeSymbol.ElementType.SpecialType != SpecialType.System_Byte &&
countArgumentArrayTypeSymbol.ElementType.SpecialType != SpecialType.System_SByte)
countArgumentArrayTypeSymbol.ElementType.SpecialType != SpecialType.System_SByte &&
countArgumentArrayTypeSymbol.ElementType.SpecialType != SpecialType.System_Boolean)
{
return true;
}
Expand All @@ -101,17 +102,17 @@ bool CheckArrayLengthLocalReference(IArgumentOperation targetArgument, IProperty
return false;
}

bool CheckLengthPropertyOnByteOrSByteArrays(IPropertyReferenceOperation countArgument)
bool CheckLengthProperty(IPropertyReferenceOperation countArgument)
{
if (countArgument.Property.Equals(arrayLengthProperty))
{
return CheckArrayLengthLocalReference(sourceArgument, countArgument) || CheckArrayLengthLocalReference(destinationArgument, countArgument);
return CheckArgumentArrayType(sourceArgument, countArgument) || CheckArgumentArrayType(destinationArgument, countArgument);
}

return false;
}

if (countArgument.Value is IPropertyReferenceOperation countArgumentValue && CheckLengthPropertyOnByteOrSByteArrays(countArgumentValue))
if (countArgument.Value is IPropertyReferenceOperation countArgumentValue && CheckLengthProperty(countArgumentValue))
{
context.ReportDiagnostic(countArgument.Value.CreateDiagnostic(Rule));
}
Expand Down Expand Up @@ -141,14 +142,14 @@ bool CheckLengthPropertyOnByteOrSByteArrays(IPropertyReferenceOperation countArg
IVariableInitializerOperation variableInitializer = variableDeclaratorOperation.Initializer;

if (variableInitializer is not null && variableInitializer.Value is IPropertyReferenceOperation variableInitializerPropertyReference &&
CheckLengthPropertyOnByteOrSByteArrays(variableInitializerPropertyReference))
CheckLengthProperty(variableInitializerPropertyReference))
{
context.ReportDiagnostic(countArgument.Value.CreateDiagnostic(Rule));
}
else if (variableDeclaratorOperation.Parent is IVariableDeclarationOperation variableDeclarationOperation &&
variableDeclarationOperation.Initializer is not null &&
variableDeclarationOperation.Initializer.Value is IPropertyReferenceOperation variableInitializerPropertyReferenceVB &&
CheckLengthPropertyOnByteOrSByteArrays(variableInitializerPropertyReferenceVB))
CheckLengthProperty(variableInitializerPropertyReferenceVB))
{
context.ReportDiagnostic(countArgument.Value.CreateDiagnostic(Rule));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,40 @@ End Module
");
}

[Theory]
[InlineData("src")]
[InlineData("dst")]
public async Task UsingBoolArray(string array)
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System;

class Program
{
static void Main()
{
bool[] src = new bool[] { true, true, true, true };
bool[] dst = new bool[] { false, false, false, false };

Buffer.BlockCopy(src, 0, dst, 0, " + array + @".Length);
}
}
");

await VerifyVB.VerifyAnalyzerAsync(@"
Imports System

Module Program
Sub Main(args As String())
Dim src = New Boolean() {True, True, True, True}
Dim dst = New Boolean() {False, False, False, False}

Buffer.BlockCopy(src, 0, dst, 0, " + array + @".Length)
End Sub
End Module
");
}

[Theory]
[InlineData("src")]
[InlineData("dst")]
Expand Down