Skip to content

Commit

Permalink
Add more UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim-Pohlmann committed Aug 2, 2024
1 parent b387ce3 commit cdc9a65
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,59 @@ private void SwitchCase(object sender, DateTimeKind e)
}
}

class NullConditionalOperatorInTry
{
private void Method(bool condition)
{
var i = 5; // Noncompliant FP
M(i += 1, i += 1); // the first one is an FP, the second a TP
// ^^^^^^ Noncompliant
// ^^^^^^ Noncompliant@-1
}

void M(int i, int j)
{
Console.WriteLine(i);
Console.WriteLine(j);
}
}

class ObjectInitializer
{
public int ID { get; set; }

void Method()
{
var x = new ObjectInitializer(); // FN
x = new ObjectInitializer { ID = 1 };
x.Method();
}
}

class TernaryInTry
{
void Method(out string param, string param2)
{
var s = ""; // FN
s = param2 switch
{
"a" => "a",
_ => "b"
};
param = s;
}
}

class OutParameter
{
void Method(out string param)
{
var s = ""; // FN
Method(out s);
param = s;
}
}

public class ReproGithubIssue697
{
private bool DoStuff() => true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,43 @@ public void Method(int a,
}
}

public class SwitchInTry
{
private int Method(int i) // Compliant
{
try
{
switch (i)
{
case 0:
return 1;
case 1:
return 2;
default:
return 3;
}
}
catch
{
return 4;
}
}
}

class NullConditionalOperatorInTry
{
private void Method(string s) // Compliant
{
try
{
s?.ToString();
}
catch
{
}
}
}

public class ReproGithubIssue2010
{
static int PatternMatch(StringSplitOptions splitOptions, int i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,25 @@ static class Extensions
public static void MyExtension(this object o) { }
}

class ObjectInitializerInTry
{
public int ID { get; set; }

void Method()
{
ObjectInitializerInTry x = null;
try
{
x = new ObjectInitializerInTry { ID = 1 };
x.Method();
}
catch
{
_ = x.ID; // Noncompliant
}
}
}

class Foo // https://github.com/SonarSource/sonar-dotnet/issues/538
{
private string bar;
Expand Down

0 comments on commit cdc9a65

Please sign in to comment.