Skip to content

Commit

Permalink
Update FN/FP test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource committed Dec 13, 2023
1 parent df211bc commit 38f2af3
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,35 @@ public static void DoSomething()
else if (myStruct.y) { } // FN
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/7057
public class Repro_7057
{
private (string, int) SomeTuple() => ("hello", 1);
private string SomeString() => "hello";

public void WithTuple()
{
string text1 = null;
(text1, var (text2, _)) = (SomeString(), SomeTuple());
if (text1 == null) // Compliant
{
Console.WriteLine();
}
if (text2 == null) // Compliant
{
Console.WriteLine();
}

string text3 = null;
((text3, _), var (text4, _)) = ((null, 42), ("hello", 42));
if (text3 == null) // Noncompliant
{
Console.WriteLine();
}
if (text4 == null) // Noncompliant
{
Console.WriteLine(); // Secondary
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public void GoGoGo()
{
var tmp = 0;
var flag = true;
while (flag) // Noncompliant
while (flag) // Compliant
{
(flag, tmp) = (false, 5);
}
Expand Down Expand Up @@ -329,7 +329,7 @@ public void MutedNull()
{
var tmp = 0;
var flag = "x";
while (flag != null) // Noncompliant
while (flag != null) // Compliant
{
(flag, tmp) = (null, 5);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ public void ParenthesizedVariableDesignation_Nested(object arg)
public void NestedDeconstructionAssignment()
{
var (a, (b, _)) = (true, (true, true));
if (a) { } // FN
if (b) { } // FN
if (a) { } // Noncompliant
if (b) { } // Noncompliant
}

int UsingDeclaration_Null()
Expand Down Expand Up @@ -330,7 +330,7 @@ public void AssignmentTarget(bool arg)
{
}

if (b) // FN
if (b) // Noncompliant
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ public object InitWithTupleAssignment
{
var tmp = 0;
var flag = true;
while (flag) // Noncompliant
while (flag) // Compliant
{
(flag, tmp) = (false, 5);
}
o = value; // Secondary
o = value;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public void Method()
List<int> list;

(list, var a) = (new List<int>(), 42);
list.Clear(); // FN
list.Clear(); // Noncompliant
list.Add(42);
list.Clear();

(var list2, var b) = (new List<int>(), 42);
list2.Clear(); // FN
list2.Clear(); // Noncompliant
list2.Add(42);
list2.Clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public void Destructuring()
int? nullable;

(nullable, _) = (null, 42);
var v = nullable.Value; // FN
var v = nullable.Value; // Noncompliant

nullable = null;
v = nullable.Value; // Noncompliant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,21 @@ class AssignmentAndDeconstruction
void TypeInference()
{
(int? discard, int? b) = (null, null);
_ = b.Value; // FN: b is empty
_ = b.Value; // Noncompliant
}

void FirstLevel()
{
var (b, _) = (null as bool?, null as bool?);
_ = b.Value; // FN: b is empty
_ = b.Value; // Noncompliant
}

void SecondLevel()
{
(int? i1, (int? i2, int? i3)) = (42, (42, null));
_ = i1.Value; // Compliant
_ = i2.Value; // Compliant
_ = i3.Value; // FN
_ = i3.Value; // Noncompliant
}

void ThirdLevel()
Expand All @@ -171,24 +171,24 @@ void ThirdLevel()
_ = i1.Value; // Compliant
_ = i2.Value; // Compliant
_ = i3.Value; // Compliant
_ = i4.Value; // FN
_ = i4.Value; // Noncompliant
_ = i5.Value; // Compliant
}

void WithDiscard()
{
(_, (int? i1, (int?, int?) _, int? i2)) = (42, (42, (42, null), null));
_ = i1.Value; // Compliant
_ = i2.Value; // FN
_ = i2.Value; // Noncompliant
}

void TwoWaySwapping()
{
bool? b1 = null;
bool? b2 = true;
(b1, b2) = (b2, b1);
_ = b1.Value; // Noncompliant, FP: after swapping is non-empty
_ = b2.Value; // FN: after swapping is empty
_ = b1.Value; // Compliant: after swapping is non-empty
_ = b2.Value; // Noncompliant
}

void ThreeWaySwapping()
Expand All @@ -197,9 +197,9 @@ void ThreeWaySwapping()
bool? b2 = true;
bool? b3 = null;
(b1, b2, b3) = (b2, b3, b2);
_ = b1.Value; // Noncompliant, FP: after swapping is non-empty
_ = b2.Value; // FN: after swapping is empty
_ = b3.Value; // Noncompliant, FP: after swapping is non-empty
_ = b1.Value; // Compliant: after swapping is non-empty
_ = b2.Value; // Noncompliant
_ = b3.Value; // Compliant: after swapping is non-empty
}

void CustomDeconstruction()
Expand All @@ -209,7 +209,7 @@ void CustomDeconstruction()
_ = i1.Value; // Compliant
_ = i2.Value; // Compliant, unknown
_ = i3.Value; // Compliant, unknown
_ = i4.Value; // FN
_ = i4.Value; // Noncompliant
}

struct DeconstructableStruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public class Sample
public void Examples(byte[] passwordBytes)
{
(var shortSalt, int a) = (new byte[15], 42);
PasswordDeriveBytes aes = new PasswordDeriveBytes(passwordBytes, shortSalt); // FN
PasswordDeriveBytes aes = new PasswordDeriveBytes(passwordBytes, shortSalt); // Noncompliant
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public void Examples()
AesCng aes = new AesCng();
aes.CreateEncryptor();
(var rgb, int a) = (new byte[16], 42);
aes.CreateEncryptor(aes.Key, rgb); // FN
aes.CreateEncryptor(aes.Key, rgb); // Noncompliant
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ public class Sample
public void Examples()
{
StringBuilder sb = new();

(sb, int a) = (null, 42);
sb.ToString(); // FN
sb.ToString(); // Noncompliant
}

public void Unassigned()
{
StringBuilder isNull, hasValue;
(isNull, hasValue) = (null, new StringBuilder());
isNull.ToString(); // FN
isNull.ToString(); // Noncompliant
hasValue.ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static void LoopOfTuples(List<IDisposable> disposables, List<(int I, IDis
disposable.Dispose(); // Compliant

foreach (var (_, disposable) in tuples)
disposable.Dispose(); // Noncompliant FP
disposable.Dispose(); // Compliant
}
}

Expand Down

0 comments on commit 38f2af3

Please sign in to comment.