Skip to content

Commit

Permalink
S3254: Add repro for #8093 and UTs for default lambda parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioaversa committed Sep 27, 2023
1 parent 059ba9c commit 7ee6fd3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public void RedundantArgument_CSharp9() =>
.WithTopLevelStatements()
.Verify();

[TestMethod]
public void RedundantArgument_CSharp12() =>
builder.AddPaths("RedundantArgument.CSharp12.cs")
.WithOptions(ParseOptionsHelper.FromCSharp12)
.Verify();

#endif

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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
// ~~
// ~~@-1
}

void NamedArguments()
{
_ = new C2(j: 42); // FN
}

class C1(int i = 42);
class C2(int i = 41, int j = 42);
}
}

0 comments on commit 7ee6fd3

Please sign in to comment.