-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.NET 8 and C# 12: add repros and UTs for rules starting with R in Son…
…arWay (#8093)
- Loading branch information
1 parent
d942723
commit 23f21a6
Showing
10 changed files
with
417 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...ts/SonarAnalyzer.UnitTest/TestCases/ParameterValidationInYieldShouldBeWrapped.CSharp12.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
class InlineArrays | ||
{ | ||
IEnumerable<int> Invalid(Buffer? buffer) // Noncompliant | ||
{ | ||
if (buffer == null) | ||
throw new ArgumentNullException(nameof(buffer)); // Secondary | ||
|
||
foreach (var item in new Buffer()) | ||
yield return item; | ||
} | ||
|
||
IEnumerable<int> Valid(Buffer? buffer) // Compliant | ||
{ | ||
if (buffer == null) | ||
throw new ArgumentNullException(nameof(buffer)); | ||
return Generator(buffer.Value); | ||
|
||
IEnumerable<int> Generator(Buffer buffer) | ||
{ | ||
foreach (var item in buffer) | ||
yield return item; | ||
} | ||
} | ||
|
||
[System.Runtime.CompilerServices.InlineArray(10)] | ||
struct Buffer | ||
{ | ||
int arrayItem; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...SonarAnalyzer.UnitTest/TestCases/RedundancyInConstructorDestructorDeclaration.CSharp12.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// https://github.com/SonarSource/sonar-dotnet/issues/8092 | ||
namespace Repro_8092 | ||
{ | ||
namespace PrimaryParameterlessConstructor | ||
{ | ||
class AClassWithBody() { } // FN | ||
class AClassWithoutBody(); // FN | ||
struct AStructWithBody() { } // FN | ||
struct AStructWithoutBody(); // FN | ||
record ARecordWithBody() { } // FN | ||
record ARecordWithoutBody(); // FN | ||
record struct ARecordStructWithBody() { } // FN | ||
record struct ARecordStructWithoutBody(); // FN | ||
|
||
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/constructor-errors?f1url=%3FappId%3Droslyn%26k%3Dk(CS8983)#constructors-in-struct-types | ||
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; | ||
} | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
analyzers/tests/SonarAnalyzer.UnitTest/TestCases/RedundantArgument.CSharp12.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
|
||
class DefaultLambdaParameters | ||
{ | ||
void SingleDefaultArgument() | ||
{ | ||
var f = (int i = 42) => i; | ||
f(); // Compliant | ||
f(40); // Compliant | ||
f(42); // Noncompliant | ||
f(41 + 1); // Noncompliant, expression results into the default value | ||
} | ||
|
||
void MultipleDefaultArguments() | ||
{ | ||
var f = (int i = 41, int j = 42) => i + j; | ||
f(); // Compliant | ||
f(42); // Compliant | ||
f(42, 42); // Noncompliant | ||
f(41, 42); // Multiple violations | ||
//^^ | ||
// ^^@-1 | ||
} | ||
|
||
void NamedArguments() | ||
{ | ||
var f = (int i = 41, int j = 42, int z = 43) => i; | ||
f(i: 42); // Error CS1746 The delegate '<anonymous delegate>' does not have a parameter named 'i' | ||
} | ||
} | ||
|
||
// https://github.com/SonarSource/sonar-dotnet/issues/8096 | ||
namespace Repro_8096 | ||
{ | ||
class PrimaryConstructors | ||
{ | ||
void SingleDefaultArgument() | ||
{ | ||
_ = new C1(); // Compliant | ||
_ = new C1(41); // Compliant | ||
_ = new C1(42); // FN | ||
_ = new C1(41 + 1); // FN, expression results into the default value | ||
} | ||
|
||
void MultipleDefaultArguments() | ||
{ | ||
_ = new C1(); // Compliant | ||
_ = new C2(42); // Compliant | ||
_ = new C2(42, 42); // FN | ||
_ = new C2(41, 42); // FN, multiple violations | ||
} | ||
|
||
void NamedArguments() | ||
{ | ||
_ = new C2(j: 42); // FN | ||
} | ||
|
||
class C1(int i = 42); | ||
class C2(int i = 41, int j = 42); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...s/tests/SonarAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.ArraySize.Fixed.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace AliasAnyType | ||
{ | ||
using Int = int; | ||
using IntArray = int[]; | ||
using IntNullable = int?; | ||
using Point = (int x, int y); | ||
using ThreeInts = int[3]; // Error [CS0270]: Array size cannot be specified in a variable declaration | ||
using IntToIntFunc = Func<int, int>; | ||
|
||
class AClass | ||
{ | ||
void AliasWithArraySize() | ||
{ | ||
IntArray a1 = new Int[] { 1, 2, 3 }; // Fixed | ||
} | ||
|
||
void AliasWithUnnecessaryType() | ||
{ | ||
IntToIntFunc f = (Int i) => i; // Fixed | ||
} | ||
|
||
void AliasWithInitializer() | ||
{ | ||
IntArray a1 = new IntArray { }; // Error [CS8386]: Invalid object creation | ||
var a2 = new IntArray { }; // Error [CS8386]: Invalid object creation | ||
int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation | ||
} | ||
|
||
void AliasWithEmptyParamsList() | ||
{ | ||
IntArray a1 = new IntArray(); // Error [CS8386]: Invalid object creation | ||
var a2 = new IntArray(); // Error [CS8386]: Invalid object creation | ||
int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation | ||
} | ||
} | ||
} | ||
|
||
// https://github.com/SonarSource/sonar-dotnet/issues/8115 | ||
namespace Repro_8115 | ||
{ | ||
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[]' | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...narAnalyzer.UnitTest/TestCases/RedundantDeclaration.CSharp12.LambdaParameterType.Fixed.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace AliasAnyType | ||
{ | ||
using Int = int; | ||
using IntArray = int[]; | ||
using IntNullable = int?; | ||
using Point = (int x, int y); | ||
using ThreeInts = int[3]; // Error [CS0270]: Array size cannot be specified in a variable declaration | ||
using IntToIntFunc = Func<int, int>; | ||
|
||
class AClass | ||
{ | ||
void AliasWithArraySize() | ||
{ | ||
IntArray a1 = new Int[3] { 1, 2, 3 }; // Fixed | ||
} | ||
|
||
void AliasWithUnnecessaryType() | ||
{ | ||
IntToIntFunc f = (i) => i; // Fixed | ||
} | ||
|
||
void AliasWithInitializer() | ||
{ | ||
IntArray a1 = new IntArray { }; // Error [CS8386]: Invalid object creation | ||
var a2 = new IntArray { }; // Error [CS8386]: Invalid object creation | ||
int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation | ||
} | ||
|
||
void AliasWithEmptyParamsList() | ||
{ | ||
IntArray a1 = new IntArray(); // Error [CS8386]: Invalid object creation | ||
var a2 = new IntArray(); // Error [CS8386]: Invalid object creation | ||
int[] a3 = new IntArray(); // Error [CS8386]: Invalid object creation | ||
} | ||
} | ||
} | ||
|
||
// https://github.com/SonarSource/sonar-dotnet/issues/8115 | ||
namespace Repro_8115 | ||
{ | ||
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[]' | ||
} | ||
} | ||
} |
Oops, something went wrong.