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

LVA: Add UTs after validation #9594

Merged
merged 1 commit into from
Aug 5, 2024
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 @@ -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 @@ -486,6 +486,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 @@ -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
Loading