Skip to content

Commit

Permalink
Code review 1
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioaversa committed Sep 29, 2023
1 parent 1f2290e commit 769ed49
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,41 @@ record ARecordWithBody() { } // FN
record ARecordWithoutBody(); // FN
record struct ARecordStructWithBody() { } // FN
record struct ARecordStructWithoutBody(); // FN

namespace FieldInitializerInStructRequiresConstructor
{
struct AStructWithFieldInitializer() // Compliant
{
public int aField = 42;
}

struct AStructWithPropertyInitializer() // Compliant
{
public int AProperty { get; } = 42;
}

record struct ARecordStructWithFieldInitializer() // Compliant
{
public int aField = 42;
}
}

namespace FieldInitializerInClassDontRequireConstructor
{
class AClassWithFieldInitializer() // FN
{
public int aField = 42;
}

class AClassWithPropertyInitializer() // FN
{
public int AProperty { get; } = 42;
}

class ARecordWithFieldInitializer() // FN
{
public int aField = 42;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ void MultipleDefaultArguments()
_ = new C2(42); // Compliant
_ = new C2(42, 42); // FN
_ = new C2(41, 42); // FN, multiple violations
// ~~
// ~~@-1
// ~~
// ~~@-1
}

void NamedArguments()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,35 @@ void AliasWithEmptyParamsList()
}
}
}

class CollectionExpressions
{
void ExplicitTypeDeclaration()
{
int[] a1 = [1, 2, 3]; // Compliant
a1 = [1, 2, 3]; // Compliant, reassignment
int[] a2 = new[] { 1, 2, 3 }; // FN, can be written as [1, 2, 3]
a2 = new[] { 1, 2, 3 }; // FN, can be written as [1, 2, 3], reassignment
}

void VarDeclarationWithInlineAssignment()
{
var invalid = [1, 2, 3]; // Error CS9176 There is no target type for the collection expression.
}

void VarDeclarationWithReassignment()
{
var typeInferredAndReassigned = new[] { 1, 2, 3 }; // Compliant, cannot be written as [1, 2, 3]
typeInferredAndReassigned = new[] { 1, 2, 3 }; // FN, can be written as [1, 2, 3], reassignment of a type-inferred variable
typeInferredAndReassigned = new int[] { 1, 2, 3 }; // Fixed
typeInferredAndReassigned = []; // Compliant
typeInferredAndReassigned = new int[] { }; // FN, can be written as []
}

void VarDeclarationWithReassignmentToEmptyCollection()
{
var typeInferredAndReassigned = new[] { 1, 2, 3 };
typeInferredAndReassigned = new[] { }; // Error CS0826 No best type found for implicitly-typed array
// Error@-1 CS0029 Cannot implicitly convert type '?[]' to 'int[]'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,35 @@ void AliasWithEmptyParamsList()
}
}
}

class CollectionExpressions
{
void ExplicitTypeDeclaration()
{
int[] a1 = [1, 2, 3]; // Compliant
a1 = [1, 2, 3]; // Compliant, reassignment
int[] a2 = new[] { 1, 2, 3 }; // FN, can be written as [1, 2, 3]
a2 = new[] { 1, 2, 3 }; // FN, can be written as [1, 2, 3], reassignment
}

void VarDeclarationWithInlineAssignment()
{
var invalid = [1, 2, 3]; // Error CS9176 There is no target type for the collection expression.
}

void VarDeclarationWithReassignment()
{
var typeInferredAndReassigned = new[] { 1, 2, 3 }; // Compliant, cannot be written as [1, 2, 3]
typeInferredAndReassigned = new[] { 1, 2, 3 }; // FN, can be written as [1, 2, 3], reassignment of a type-inferred variable
typeInferredAndReassigned = new int[] { 1, 2, 3 }; // Fixed
typeInferredAndReassigned = []; // Compliant
typeInferredAndReassigned = new int[] { }; // FN, can be written as []
}

void VarDeclarationWithReassignmentToEmptyCollection()
{
var typeInferredAndReassigned = new[] { 1, 2, 3 };
typeInferredAndReassigned = new[] { }; // Error CS0826 No best type found for implicitly-typed array
// Error@-1 CS0029 Cannot implicitly convert type '?[]' to 'int[]'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,35 @@ void AliasWithEmptyParamsList()
}
}
}

class CollectionExpressions
{
void ExplicitTypeDeclaration()
{
int[] a1 = [1, 2, 3]; // Compliant
a1 = [1, 2, 3]; // Compliant, reassignment
int[] a2 = new[] { 1, 2, 3 }; // FN, can be written as [1, 2, 3]
a2 = new[] { 1, 2, 3 }; // FN, can be written as [1, 2, 3], reassignment
}

void VarDeclarationWithInlineAssignment()
{
var invalid = [1, 2, 3]; // Error CS9176 There is no target type for the collection expression.
}

void VarDeclarationWithReassignment()
{
var typeInferredAndReassigned = new[] { 1, 2, 3 }; // Compliant, cannot be written as [1, 2, 3]
typeInferredAndReassigned = new[] { 1, 2, 3 }; // FN, can be written as [1, 2, 3], reassignment of a type-inferred variable
typeInferredAndReassigned = new int[] { 1, 2, 3 }; // Noncompliant, can be written as [1, 2, 3]
typeInferredAndReassigned = []; // Compliant
typeInferredAndReassigned = new int[] { }; // FN, can be written as []
}

void VarDeclarationWithReassignmentToEmptyCollection()
{
var typeInferredAndReassigned = new[] { 1, 2, 3 };
typeInferredAndReassigned = new[] { }; // Error CS0826 No best type found for implicitly-typed array
// Error@-1 CS0029 Cannot implicitly convert type '?[]' to 'int[]'
}
}

0 comments on commit 769ed49

Please sign in to comment.