-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
059ba9c
commit 7ee6fd3
Showing
2 changed files
with
69 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
63 changes: 63 additions & 0 deletions
63
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,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); | ||
} | ||
} |