Skip to content

Commit

Permalink
.NET 8 and C# 12: add repros and UTs for rules starting with P in Son…
Browse files Browse the repository at this point in the history
…arWay (#8082)
  • Loading branch information
antonioaversa authored Sep 29, 2023
1 parent f98d73d commit 1fd123f
Show file tree
Hide file tree
Showing 13 changed files with 461 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public void ParametersCorrectOrder_CSharp8() =>
public void ParametersCorrectOrder_CSharp11() =>
builderCS.AddPaths("ParametersCorrectOrder.CSharp11.cs").WithOptions(ParseOptionsHelper.FromCSharp11).Verify();

[TestMethod]
public void ParametersCorrectOrder_CSharp12() =>
builderCS.AddPaths("ParametersCorrectOrder.CSharp12.cs").WithOptions(ParseOptionsHelper.FromCSharp12).Verify();

#endif

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public void PartCreationPolicyShouldBeUsedWithExportAttribute_CSharp9() =>
.WithOptions(ParseOptionsHelper.FromCSharp9)
.Verify();

[TestMethod]
public void PartCreationPolicyShouldBeUsedWithExportAttribute_CSharp12() =>
builderCS.AddPaths("PartCreationPolicyShouldBeUsedWithExportAttribute.CSharp12.cs")
.WithOptions(ParseOptionsHelper.FromCSharp12)
.Verify();

#endif

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,37 @@ public class PropertiesAccessCorrectFieldTest

[TestMethod]
public void PropertiesAccessCorrectField_CS() =>
builderCS.AddPaths(@"PropertiesAccessCorrectField.cs").AddReferences(AdditionalReferences).Verify();
builderCS.AddPaths("PropertiesAccessCorrectField.cs").AddReferences(AdditionalReferences).Verify();

[TestMethod]
public void PropertiesAccessCorrectField_CSharp8() =>
builderCS.AddPaths(@"PropertiesAccessCorrectField.CSharp8.cs").WithOptions(ParseOptionsHelper.FromCSharp8).Verify();
builderCS.AddPaths("PropertiesAccessCorrectField.CSharp8.cs").WithOptions(ParseOptionsHelper.FromCSharp8).Verify();

#if NET

[TestMethod]
public void PropertiesAccessCorrectField_CSharp9() =>
builderCS.AddPaths(@"PropertiesAccessCorrectField.CSharp9.cs").WithOptions(ParseOptionsHelper.FromCSharp9).Verify();
builderCS.AddPaths("PropertiesAccessCorrectField.CSharp9.cs").WithOptions(ParseOptionsHelper.FromCSharp9).Verify();

[TestMethod]
public void PropertiesAccessCorrectField_CSharp12() =>
builderCS.AddPaths("PropertiesAccessCorrectField.CSharp12.cs").WithOptions(ParseOptionsHelper.FromCSharp12).Verify();

#else

[TestMethod]
public void PropertiesAccessCorrectField_CS_NetFramework() =>
builderCS.AddPaths(@"PropertiesAccessCorrectField.NetFramework.cs").AddReferences(AdditionalReferences).Verify();
builderCS.AddPaths("PropertiesAccessCorrectField.NetFramework.cs").AddReferences(AdditionalReferences).Verify();

[TestMethod]
public void PropertiesAccessCorrectField_VB_NetFramework() =>
builderVB.AddPaths(@"PropertiesAccessCorrectField.NetFramework.vb").AddReferences(AdditionalReferences).Verify();
builderVB.AddPaths("PropertiesAccessCorrectField.NetFramework.vb").AddReferences(AdditionalReferences).Verify();

#endif

[TestMethod]
public void PropertiesAccessCorrectField_VB() =>
builderVB.AddPaths(@"PropertiesAccessCorrectField.vb").AddReferences(AdditionalReferences).Verify();
builderVB.AddPaths("PropertiesAccessCorrectField.vb").AddReferences(AdditionalReferences).Verify();

private static IEnumerable<MetadataReference> AdditionalReferences =>
NuGetMetadataReference.MvvmLightLibs("5.4.1.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public void PublicMethodWithMultidimensionalArray_CSharp11() =>
.WithOptions(ParseOptionsHelper.FromCSharp11)
.Verify();

[TestMethod]
public void PublicMethodWithMultidimensionalArray_CSharp12() =>
builderCS.AddPaths("PublicMethodWithMultidimensionalArray.CSharp12.cs")
.WithOptions(ParseOptionsHelper.FromCSharp12)
.Verify();

#endif

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,39 @@ public static IEnumerable<int> WithFunc(string foo)
}
}
}

class NullCoalescing
{
IEnumerable<int> Invalid(int? i) // FN
{
_ = i ?? throw new ArgumentNullException(nameof(i));

yield return 1;
}

IEnumerable<int> ValidWithSecondMethod(int? i) // Compliant
{
_ = i ?? throw new ArgumentNullException(nameof(i));
return SecondMethod();
}

IEnumerable<int> SecondMethod() { yield return 1; }

IEnumerable<int> ValidWithLocalFunction(int? i) // Compliant
{
_ = i ?? throw new ArgumentNullException(nameof(i));
return LocalFunction();

IEnumerable<int> LocalFunction() { yield return 1; }
}
}

class NullCoalescingAssignment
{
IEnumerable<int> NullableInlineArray(int? i) // FN
{
_ = i ?? throw new ArgumentNullException(nameof(i));

yield return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,43 @@ public SomeClass()
}
}
}

namespace Repro_8072
{
public class Lambdas
{
Func<int, int, int> F1 = (int a, int b) => a + b;

void InvokedFromAnotherLambda()
{
var f1 = (int a, int b) => a + b;
var paramsFullyInverted = (int a, int b) => f1(b, a); // FN
var paramsFullyInvertedWithAdditionalParamAfter = (int a, int b, string s) => f1(b, a); // FN
var paramsFullyInvertedWithAdditionalParamBefore = (string s, int a, int b) => f1(b, a); // FN

var f2 = (int a, int b, int c) => a + b + c;
var paramsPartiallyInvertedFirstAndSecond = (int a, int b, int c) => f2(b, a, c); // FN
var paramsPartiallyInvertedFirstAndLast = (int a, int b, int c) => f2(c, b, a); // FN
var paramsPartiallyInvertedSecondAndLast = (int a, int b, int c) => f2(a, c, b); // FN
}

void InvokedFromLocalFunction()
{
var f1 = (int a, int b) => a + b;
var f2 = (int a, int b, int c) => a + b + c;

int FullyInverted(int a, int b) => f1(b, a);
int FullyInvertedWithAdditionalParamAfter(int a, int b, string c) => f1(b, a);
int FullyInvertedWithAdditionalParamBefore(string c, int a, int b) => f1(b, a);

int PartiallyInvertedFirstAndSecond(int a, int b, int c) => f2(b, a, c); // FN
int PartiallyInvertedFirstAndLast(int a, int b, int c) => f2(c, b, a); // FN
int PartiallyInvertedSecondAndLast(int a, int b, int c) => f2(a, c, b); // FN
}

void InvokedFromAMethod(int a, int b)
{
F1(b, a); // FN
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;

namespace Tests.Diagnostics
{
public interface IInterface
{
static virtual void SomeMethod(int a, int b) { } // Secondary
}

public class SomeClass<T> where T : IInterface
{
public SomeClass()
{
int a = 1;
int b = 2;

T.SomeMethod(b, a); // Noncompliant
}
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/8071
namespace Repro_8071
{
class BaseConstructor
{
class Base(int a, int b)
{
Base(int a, int b, string c) : this(b, a) { } // FN: ctor params inverted with additional param after
Base(string c, int a, int b) : this(b, a) { } // FN: ctor params inverted with additional param before
}

class ParamsFullyInverted(int a, int b) : Base(b, a); // FN
class ParamsPartiallyInverted(int a, int b, int c) : Base(b, a); // FN
class ParamsFullyInvertedWithAdditionalParamAfter(int a, int b, string s) : Base(b, a); // FN
class ParamsFullyInvertedWithAdditionalParamBefore(string s, int a, int b) : Base(b, a); // FN
}

class WithRecordStructs
{
void Basics(int a, int b, int c)
{
_ = new SomeRecord(b, a); // Noncompliant
}

void WithPromotion(short a, short b)
{
_ = new SomeRecord(b, a); // Noncompliant
}

void WithCasting(long a, long b)
{
_ = new SomeRecord((int)b, (int)a); // FN
}

record SomeRecord(int a, int b)
{
public SomeRecord(int a, int b, string c) : this(b, a) { } // FN
public SomeRecord(string c, int a, int b) : this(b, a) { } // FN
}
}

class WithRecords
{
void Basics(int a, int b, int c)
{
_ = new SomeRecordStruct(b, a); // Noncompliant
}

void WithPromotion(short a, short b)
{
_ = new SomeRecordStruct(b, a); // Noncompliant
}

void WithCasting(long a, long b)
{
_ = new SomeRecordStruct((int)b, (int)a); // FN
}

record struct SomeRecordStruct(int a, int b)
{
public SomeRecordStruct(int a, int b, string c) : this(b, a) { } // FN
public SomeRecordStruct(string c, int a, int b) : this(b, a) { } // FN
}
}
}

namespace Repro_8072
{
public class DefaultLambdaParameters
{
void InvokedFromAnotherLambda()
{
var f1 = (int a, int b) => a + b;
var paramsFullyInverted = (int a, int b) => f1(b, a); // FN
var paramsFullyInvertedWithAdditionalParamAfter = (int a, int b, string s) => f1(b, a); // FN
var paramsFullyInvertedWithAdditionalParamBefore = (string s, int a, int b) => f1(b, a); // FN

var f2 = (int a, int b, int c) => a + b + c;
var paramsPartiallyInvertedFirstAndSecond = (int a, int b, int c) => f2(b, a, c); // FN
var paramsPartiallyInvertedFirstAndLast = (int a, int b, int c) => f2(c, b, a); // FN
var paramsPartiallyInvertedSecondAndLast = (int a, int b, int c) => f2(a, c, b); // FN
}

void InvokedFromLocalFunction()
{
var f = (int a, int b) => a + b;

int SomeLocalFunction(int a, int b) => f(b, a);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using static Tests.Diagnostics.A;

namespace Tests.Diagnostics
{
Expand Down Expand Up @@ -194,3 +196,62 @@ public void M3()
}
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/8070
namespace Repro_8070
{
class InvokingConstructorViaNew
{
void Test(int a, int b, int c)
{
_ = new SomeClass(b, a); // Noncompliant, fully inverted
_ = new SomeClass(b, a, c); // Noncompliant, partially inverted
}

class SomeClass
{
public SomeClass(int a, int b) { } // Secondary
public SomeClass(int a, int b, int c) { } // Secondary
}
}

class InvokingConstructorViaThis
{
class SomeClass
{
public SomeClass(int a, int b) { }
public SomeClass(int a, int b, string c) : this(b, a) { } // FN: ctor params fully inverted with additional par after
public SomeClass(string c, int a, int b) : this(b, a) { } // FN: ctor params fully inverted with additional par before
}
}

class InvokingConstructorViaBase
{
class Base
{
public Base(int a, int b) { }
public Base(int a, int b, int c) { }
}

class ParamsFullyInverted : Base
{
public ParamsFullyInverted(int a, int b) : base(b, a) { } // FN
}

class ParamsPartiallyInverted : Base
{
public ParamsPartiallyInverted(int a, int b, int c) : base(b, a, c) { } // FN
}

class ParamsFullyInvertedWithAdditionalParamAfter : Base
{
public ParamsFullyInvertedWithAdditionalParamAfter(int a, int b, string s) : base(b, a) { } // FN
}

class ParamsFullyInvertedWithAdditionalParamBefore : Base
{
public ParamsFullyInvertedWithAdditionalParamBefore(string s, int a, int b) : base(b, a) { } // FN
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.ComponentModel.Composition;

class PrimaryConstructors
{
[PartCreationPolicy(CreationPolicy.Any)] // Noncompliant
class Class1(int iPar, in int iParWithExplicitIn, int iParWithDefault = 42);

[InheritedExport(typeof(object))]
[PartCreationPolicy(CreationPolicy.Any)] // Compliant, InheritedExport is present
class Class2(int iPar, in int iParWithExplicitIn, int iParWithDefault = 42);
}
Loading

0 comments on commit 1fd123f

Please sign in to comment.