Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add repro for #8070, #8071, #8072
Browse files Browse the repository at this point in the history
antonioaversa committed Sep 26, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent b8607d2 commit d05385a
Showing 4 changed files with 217 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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
@@ -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
{
@@ -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
}
}
}

0 comments on commit d05385a

Please sign in to comment.