Merge pull request #6800 from hvitved/csharp/constant-cond-tuple-discard

C#: Filter discards in tuples in `ConstantCondition.ql`
This commit is contained in:
Tom Hvitved
2021-10-04 14:38:45 +02:00
committed by GitHub
4 changed files with 32 additions and 17 deletions

View File

@@ -48,8 +48,8 @@ class ConstantNullness
j = (int?)i ?? 1; // BAD
s = ""?.CommaJoinWith(s); // BAD
s = s ?? ""; // GOOD
s = (i==0 ? s : null) ?? s; // GOOD
var k = (i==0 ? s : null)?.Length; // GOOD
s = (i == 0 ? s : null) ?? s; // GOOD
var k = (i == 0 ? s : null)?.Length; // GOOD
}
}
@@ -59,12 +59,12 @@ class ConstantMatching
{
switch (1 + 2)
{
case 2 : // BAD
break;
case 3 : // BAD
break;
case int _ : // GOOD
break;
case 2: // BAD
break;
case 3: // BAD
break;
case int _: // GOOD
break;
}
}
@@ -72,10 +72,10 @@ class ConstantMatching
{
switch ((object)s)
{
case int _ : // BAD
break;
case "" : // GOOD
break;
case int _: // BAD
break;
case "": // GOOD
break;
}
}
@@ -83,8 +83,8 @@ class ConstantMatching
{
switch (o)
{
case IList _ : // GOOD
break;
case IList _: // GOOD
break;
}
}
@@ -105,7 +105,8 @@ class ConstantMatching
};
}
void M6(bool b1, bool b2) {
void M6(bool b1, bool b2)
{
if (!b1)
return;
if (!b2)
@@ -113,6 +114,16 @@ class ConstantMatching
if (b1 && b2) // BAD
return;
}
string M7(object o)
{
return o switch
{
(string s, _) => s, // GOOD
(_, string s) => s, // GOOD
_ => "" // GOOD
};
}
}
class Assertions

View File

@@ -8,8 +8,8 @@
| ConstantCondition.cs:64:18:64:18 | 3 | Pattern always matches. |
| ConstantCondition.cs:75:18:75:20 | access to type Int32 | Pattern never matches. |
| ConstantCondition.cs:95:13:95:13 | _ | Pattern always matches. |
| ConstantCondition.cs:113:13:113:14 | access to parameter b1 | Condition always evaluates to 'true'. |
| ConstantCondition.cs:113:19:113:20 | access to parameter b2 | Condition always evaluates to 'true'. |
| ConstantCondition.cs:114:13:114:14 | access to parameter b1 | Condition always evaluates to 'true'. |
| ConstantCondition.cs:114:19:114:20 | access to parameter b2 | Condition always evaluates to 'true'. |
| ConstantConditionBad.cs:5:16:5:20 | ... > ... | Condition always evaluates to 'false'. |
| ConstantConditionalExpressionCondition.cs:11:22:11:34 | ... == ... | Condition always evaluates to 'true'. |
| ConstantConditionalExpressionCondition.cs:12:21:12:25 | false | Condition always evaluates to 'false'. |