Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET 8 and C# 12: fix repros and UTs for rules starting with P in SonarWay #8118

Merged
merged 8 commits into from
Oct 2, 2023
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 @@ -139,13 +139,3 @@ IEnumerable<int> ValidWithLocalFunction(int? i) // Compliant
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 @@ -21,7 +21,7 @@ public SomeClass()

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

Expand All @@ -43,9 +43,9 @@ 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 FullyInverted(int a, int b) => f1(b, a); // FN
int FullyInvertedWithAdditionalParamAfter(int a, int b, string c) => f1(b, a); // FN
int FullyInvertedWithAdditionalParamBefore(string c, int a, int b) => f1(b, a); // FN

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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
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
{
Expand Down Expand Up @@ -91,22 +72,22 @@ public class DefaultLambdaParameters
{
void InvokedFromAnotherLambda()
{
var f1 = (int a, int b) => a + b;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#8093 (comment)

These tests were not supposed to be duplicates of the ones for C# 11, but rather tests with the same structure, but having default lambda parameters set, that makes them C# 12 specific.

I have kept the C# 12 testa and added the missing default values to the lambda parameters.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to mention the default lambda parameters in #8072 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I have added a sentence in the description of the GitHub issue.

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
var f1 = (int a = 42, int b = 42) => a + b;
var paramsFullyInverted = (int a = 42, int b = 42) => f1(b, a); // FN
var paramsFullyInvertedWithAdditionalParamAfter = (int a = 42, int b = 42, string s = "42") => f1(b, a); // FN
var paramsFullyInvertedWithAdditionalParamBefore = (string s = "42", int a = 42, int b = 42) => f1(b, a); // FN

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

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

int SomeLocalFunction(int a, int b) => f(b, a);
int SomeLocalFunction(int a = 42, int b = 42) => f(b, a); // FN
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using static Tests.Diagnostics.A;

namespace Tests.Diagnostics
{
Expand Down