mirror of
https://github.com/github/codeql.git
synced 2026-04-25 00:35:20 +02:00
C#: Convert cs/constant-condition tests to inline expectation tests.
This commit is contained in:
@@ -35,18 +35,18 @@ class ConstantNullness
|
||||
{
|
||||
void M1(int i)
|
||||
{
|
||||
var j = ((string)null)?.Length; // BAD
|
||||
var s = ((int?)i)?.ToString(); // BAD
|
||||
var j = ((string)null)?.Length; // $ Alert
|
||||
var s = ((int?)i)?.ToString(); // $ Alert
|
||||
var k = s?.Length; // GOOD
|
||||
k = s?.ToLower()?.Length; // GOOD
|
||||
}
|
||||
|
||||
void M2(int i)
|
||||
{
|
||||
var j = (int?)null ?? 0; // BAD
|
||||
var s = "" ?? "a"; // BAD
|
||||
j = (int?)i ?? 1; // BAD
|
||||
s = ""?.CommaJoinWith(s); // BAD
|
||||
var j = (int?)null ?? 0; // $ Alert
|
||||
var s = "" ?? "a"; // $ Alert
|
||||
j = (int?)i ?? 1; // $ Alert
|
||||
s = ""?.CommaJoinWith(s); // $ Alert
|
||||
s = s ?? ""; // GOOD
|
||||
s = (i == 0 ? s : null) ?? s; // GOOD
|
||||
var k = (i == 0 ? s : null)?.Length; // GOOD
|
||||
@@ -59,9 +59,9 @@ class ConstantMatching
|
||||
{
|
||||
switch (1 + 2)
|
||||
{
|
||||
case 2: // BAD
|
||||
case 2: // $ Alert
|
||||
break;
|
||||
case 3: // BAD
|
||||
case 3: // $ Alert
|
||||
break;
|
||||
case int _: // GOOD
|
||||
break;
|
||||
@@ -72,7 +72,7 @@ class ConstantMatching
|
||||
{
|
||||
switch ((object)s)
|
||||
{
|
||||
case int _: // BAD
|
||||
case int _: // $ Alert
|
||||
break;
|
||||
case "": // GOOD
|
||||
break;
|
||||
@@ -92,7 +92,7 @@ class ConstantMatching
|
||||
{
|
||||
return o switch
|
||||
{
|
||||
_ => o.ToString() // BAD
|
||||
_ => o.ToString() // $ Alert
|
||||
};
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ class ConstantMatching
|
||||
return;
|
||||
if (!b2)
|
||||
return;
|
||||
if (b1 && b2) // BAD
|
||||
if (b1 && b2) // $ Alert
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Bad Practices/Control-Flow/ConstantCondition.ql
|
||||
query: Bad Practices/Control-Flow/ConstantCondition.ql
|
||||
postprocess: utils/test/InlineExpectationsTestQuery.ql
|
||||
|
||||
@@ -2,6 +2,6 @@ class Bad
|
||||
{
|
||||
public int Max(int a, int b)
|
||||
{
|
||||
return a > a ? a : b;
|
||||
return a > a ? a : b; // $ Alert
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ namespace ConstantConditionalExpression
|
||||
|
||||
public void Foo()
|
||||
{
|
||||
int i = (ZERO == 1 - 1) ? 0 : 1; // BAD
|
||||
int j = false ? 0 : 1; // BAD
|
||||
int k = " " == " " ? 0 : 1; // BAD
|
||||
int l = (" "[0] == ' ') ? 0 : 1; // BAD: but not flagged
|
||||
int i = (ZERO == 1 - 1) ? 0 : 1; // $ Alert
|
||||
int j = false ? 0 : 1; // $ Alert
|
||||
int k = " " == " " ? 0 : 1; // $ Alert
|
||||
int l = (" "[0] == ' ') ? 0 : 1; // Missing Alert
|
||||
int m = Bar() == 0 ? 0 : 1; // GOOD
|
||||
}
|
||||
|
||||
@@ -21,5 +21,4 @@ namespace ConstantConditionalExpression
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ namespace ConstantForCondition
|
||||
{
|
||||
public void M()
|
||||
{
|
||||
for (int i = 0; false; i++) // GOOD
|
||||
for (int i = 0; false; i++) // $ Alert
|
||||
;
|
||||
for (int i = 0; 0 == 1; i++) // BAD
|
||||
for (int i = 0; 0 == 1; i++) // $ Alert
|
||||
;
|
||||
for (; ; ) // GOOD
|
||||
;
|
||||
|
||||
@@ -8,20 +8,20 @@ namespace ConstantIfCondition
|
||||
|
||||
public void Foo()
|
||||
{
|
||||
if (ZERO == 1 - 1)
|
||||
{ // BAD
|
||||
if (ZERO == 1 - 1) // $ Alert
|
||||
{
|
||||
}
|
||||
if (false)
|
||||
{ // BAD
|
||||
if (false) // $ Alert
|
||||
{
|
||||
}
|
||||
if (" " == " ")
|
||||
{ // BAD
|
||||
if (" " == " ") // $ Alert
|
||||
{
|
||||
}
|
||||
if (" "[0] == ' ')
|
||||
{ // BAD: but not flagged
|
||||
if (" "[0] == ' ') // Missing Alert
|
||||
{
|
||||
}
|
||||
if (Bar() == 0)
|
||||
{ // GOOD
|
||||
if (Bar() == 0) // GOOD
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,17 +7,17 @@ namespace ConstantIsNullOrEmpty
|
||||
static void Main(string[] args)
|
||||
{
|
||||
{
|
||||
if (string.IsNullOrEmpty(nameof(args))) // bad: always false
|
||||
if (string.IsNullOrEmpty(nameof(args))) // $ Alert
|
||||
{
|
||||
}
|
||||
|
||||
string? x = null;
|
||||
if (string.IsNullOrEmpty(x)) // would be nice... bad: always true
|
||||
if (string.IsNullOrEmpty(x)) // Missing Alert (always true)
|
||||
{
|
||||
}
|
||||
|
||||
string y = "";
|
||||
if (string.IsNullOrEmpty(y)) // would be nice... bad: always true
|
||||
if (string.IsNullOrEmpty(y)) // Missing Alert (always true)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -28,12 +28,12 @@ namespace ConstantIsNullOrEmpty
|
||||
}
|
||||
|
||||
string z = " ";
|
||||
if (string.IsNullOrEmpty(z)) // would be nice... bad: always false
|
||||
if (string.IsNullOrEmpty(z)) // Missing Alert (always false)
|
||||
{
|
||||
}
|
||||
|
||||
string a = "a";
|
||||
if (string.IsNullOrEmpty(a)) // would be nice... bad: always false
|
||||
if (string.IsNullOrEmpty(a)) // Missing Alert (always false)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -43,18 +43,18 @@ namespace ConstantIsNullOrEmpty
|
||||
{
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(null)) // bad: always true
|
||||
if (string.IsNullOrEmpty(null)) // $ Alert
|
||||
{
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty("")) // bad: always true
|
||||
if (string.IsNullOrEmpty("")) // $ Alert
|
||||
{
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(" ")) // bad: always false
|
||||
if (string.IsNullOrEmpty(" ")) // $ Alert
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ namespace ConstantNullCoalescingLeftHandOperand
|
||||
|
||||
public void Foo()
|
||||
{
|
||||
object i = NULL_OBJECT ?? ""; // BAD
|
||||
object j = null ?? ""; // BAD
|
||||
object i = NULL_OBJECT ?? ""; // $ Alert
|
||||
object j = null ?? ""; // $ Alert
|
||||
object k = Bar() ?? ""; // GOOD
|
||||
}
|
||||
|
||||
|
||||
@@ -9,28 +9,28 @@ namespace ConstantWhileCondition
|
||||
|
||||
public void Foo()
|
||||
{
|
||||
while (ZERO == 1 - 1)
|
||||
{ // BAD
|
||||
while (ZERO == 1 - 1) // $ Alert
|
||||
{
|
||||
break;
|
||||
}
|
||||
while (false)
|
||||
{ // GOOD
|
||||
while (false) // $ Alert
|
||||
{
|
||||
break;
|
||||
}
|
||||
while (true)
|
||||
{ // GOOD
|
||||
while (true) // GOOD
|
||||
{
|
||||
break;
|
||||
}
|
||||
while (" " == " ")
|
||||
{ // BAD
|
||||
while (" " == " ") // $ Alert
|
||||
{
|
||||
break;
|
||||
}
|
||||
while (" "[0] == ' ')
|
||||
{ // BAD: but not flagged
|
||||
while (" "[0] == ' ') // Missing Alert
|
||||
{
|
||||
break;
|
||||
}
|
||||
while (Bar() == 0)
|
||||
{ // GOOD
|
||||
while (Bar() == 0) // GOOD
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user