From 4451e55bba7d172161811411537d802fa802b30f Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 11 Mar 2025 13:35:05 +0100 Subject: [PATCH 1/8] C#: Convert cs/constant-condition tests to inline expectation tests. --- .../ConstantCondition/ConstantCondition.cs | 22 ++++++++--------- .../ConstantCondition/ConstantCondition.qlref | 3 ++- .../ConstantCondition/ConstantConditionBad.cs | 2 +- .../ConstantConditionalExpressionCondition.cs | 9 ++++--- .../ConstantCondition/ConstantForCondition.cs | 4 ++-- .../ConstantCondition/ConstantIfCondition.cs | 20 ++++++++-------- .../ConstantIsNullOrEmpty.cs | 18 +++++++------- .../ConstantNullCoalescingLeftHandOperand.cs | 4 ++-- .../ConstantWhileCondition.cs | 24 +++++++++---------- 9 files changed, 53 insertions(+), 53 deletions(-) diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs index 9e7386149a40..d472ca599121 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs @@ -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 @@ void M1() { 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 @@ void M2(string s) { switch ((object)s) { - case int _: // BAD + case int _: // $ Alert break; case "": // GOOD break; @@ -92,7 +92,7 @@ string M4(object o) { return o switch { - _ => o.ToString() // BAD + _ => o.ToString() // $ Alert }; } @@ -111,7 +111,7 @@ void M6(bool b1, bool b2) return; if (!b2) return; - if (b1 && b2) // BAD + if (b1 && b2) // $ Alert return; } diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.qlref b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.qlref index 1fa68b335bbb..6692217230e0 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.qlref +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.qlref @@ -1 +1,2 @@ -Bad Practices/Control-Flow/ConstantCondition.ql \ No newline at end of file +query: Bad Practices/Control-Flow/ConstantCondition.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs index bd1e44b346b5..66af12c057af 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs @@ -2,6 +2,6 @@ class Bad { public int Max(int a, int b) { - return a > a ? a : b; + return a > a ? a : b; // $ Alert } } diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionalExpressionCondition.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionalExpressionCondition.cs index 1ee318becbd2..4cd56232627d 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionalExpressionCondition.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionalExpressionCondition.cs @@ -8,10 +8,10 @@ class Main 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 @@ public int Bar() } } - } diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantForCondition.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantForCondition.cs index 7ccb9ac86c74..2da0589d1827 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantForCondition.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantForCondition.cs @@ -6,9 +6,9 @@ class Main { 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 ; diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs index 44869e51af0b..04c91cc222da 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs @@ -8,20 +8,20 @@ class Main 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 + { } } diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIsNullOrEmpty.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIsNullOrEmpty.cs index 5cad2e818abe..01e8353a20f4 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIsNullOrEmpty.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIsNullOrEmpty.cs @@ -7,17 +7,17 @@ internal class Program 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 @@ static void Main(string[] args) } 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 @@ static void Main(string[] args) { } - 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 { } } } } -} \ No newline at end of file +} diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantNullCoalescingLeftHandOperand.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantNullCoalescingLeftHandOperand.cs index fa2ee7d00b0f..6901daf643ed 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantNullCoalescingLeftHandOperand.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantNullCoalescingLeftHandOperand.cs @@ -8,8 +8,8 @@ class Main 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 } diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantWhileCondition.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantWhileCondition.cs index 64dc8150d56e..59575e0de45e 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantWhileCondition.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantWhileCondition.cs @@ -9,28 +9,28 @@ class Main 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; } } From f42ae48ffa01ace1dabc7ad39bff2326f6f0c1cb Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 11 Mar 2025 14:35:04 +0100 Subject: [PATCH 2/8] C#: Add some switch case examples. --- .../ConstantCondition/ConstantCondition.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs index d472ca599121..0445e152ec72 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs @@ -124,6 +124,35 @@ string M7(object o) _ => "" // GOOD }; } + + string M8(int i) + { + return i switch + { + _ when i % 2 == 0 => "even", // GOOD + _ => "odd" // GOOD + }; + } + + string M9(int i) + { + switch (i) + { + case var _: // $ Alert + return "even"; + } + } + + string M10(int i) + { + switch (i) + { + case var _ when i % 2 == 0: // GOOD + return "even"; + case var _: // GOOD + return "odd"; + } + } } class Assertions From c15137e99284c133c6249cb5f78b618bab4dd694 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 11 Mar 2025 14:36:34 +0100 Subject: [PATCH 3/8] C#: Update test expected output. --- .../ConstantCondition/ConstantCondition.expected | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected index 397d77531b29..37137aa00181 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected @@ -1,3 +1,4 @@ +#select | ConstantCondition.cs:38:18:38:29 | (...) ... | Expression is always 'null'. | | ConstantCondition.cs:39:18:39:24 | (...) ... | Expression is never 'null'. | | ConstantCondition.cs:46:17:46:26 | (...) ... | Expression is always 'null'. | @@ -10,6 +11,10 @@ | ConstantCondition.cs:95:13:95:13 | _ | Pattern always matches. | | 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'. | +| ConstantCondition.cs:132:13:132:13 | _ | Pattern always matches. | +| ConstantCondition.cs:141:22:141:22 | _ | Pattern always matches. | +| ConstantCondition.cs:150:22:150:22 | _ | Pattern always matches. | +| ConstantCondition.cs:152:22:152:22 | _ | Pattern always matches. | | 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'. | @@ -28,3 +33,7 @@ | ConstantWhileCondition.cs:12:20:12:32 | ... == ... | Condition always evaluates to 'true'. | | ConstantWhileCondition.cs:16:20:16:24 | false | Condition always evaluates to 'false'. | | ConstantWhileCondition.cs:24:20:24:29 | ... == ... | Condition always evaluates to 'true'. | +testFailures +| ConstantCondition.cs:132:13:132:13 | Pattern always matches. | Unexpected result: Alert | +| ConstantCondition.cs:150:22:150:22 | Pattern always matches. | Unexpected result: Alert | +| ConstantCondition.cs:152:22:152:22 | Pattern always matches. | Unexpected result: Alert | From 150aa5d1cf2067b415a2abd38b96cfbdf2c137a2 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 11 Mar 2025 14:37:56 +0100 Subject: [PATCH 4/8] C#: Include normal switch/case statements in the white list and allow the use of wildcards when there is a condition. --- .../Bad Practices/Control-Flow/ConstantCondition.ql | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql b/csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql index a0542a947356..6c717bfcb4c1 100644 --- a/csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql +++ b/csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql @@ -119,9 +119,14 @@ class ConstantMatchingCondition extends ConstantCondition { } override predicate isWhiteListed() { - exists(SwitchExpr se, int i | - se.getCase(i).getPattern() = this.(DiscardExpr) and - i > 0 + exists(Switch se, Case c, int i | c = se.getCase(i) | + c.getPattern() = this.(DiscardExpr) and + ( + i > 0 + or + i = 0 and + exists(Expr cond | c.getCondition() = cond and not isConstantCondition(cond, true)) + ) ) or this = any(PositionalPatternExpr ppe).getPattern(_) From 120af3611ab13829a3d190db5c7783a73e01e55c Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 11 Mar 2025 14:39:09 +0100 Subject: [PATCH 5/8] C#: Update test expected output. --- .../ConstantCondition/ConstantCondition.expected | 8 -------- 1 file changed, 8 deletions(-) diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected index 37137aa00181..e154d10b9d3a 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected @@ -1,4 +1,3 @@ -#select | ConstantCondition.cs:38:18:38:29 | (...) ... | Expression is always 'null'. | | ConstantCondition.cs:39:18:39:24 | (...) ... | Expression is never 'null'. | | ConstantCondition.cs:46:17:46:26 | (...) ... | Expression is always 'null'. | @@ -11,10 +10,7 @@ | ConstantCondition.cs:95:13:95:13 | _ | Pattern always matches. | | 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'. | -| ConstantCondition.cs:132:13:132:13 | _ | Pattern always matches. | | ConstantCondition.cs:141:22:141:22 | _ | Pattern always matches. | -| ConstantCondition.cs:150:22:150:22 | _ | Pattern always matches. | -| ConstantCondition.cs:152:22:152:22 | _ | Pattern always matches. | | 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'. | @@ -33,7 +29,3 @@ | ConstantWhileCondition.cs:12:20:12:32 | ... == ... | Condition always evaluates to 'true'. | | ConstantWhileCondition.cs:16:20:16:24 | false | Condition always evaluates to 'false'. | | ConstantWhileCondition.cs:24:20:24:29 | ... == ... | Condition always evaluates to 'true'. | -testFailures -| ConstantCondition.cs:132:13:132:13 | Pattern always matches. | Unexpected result: Alert | -| ConstantCondition.cs:150:22:150:22 | Pattern always matches. | Unexpected result: Alert | -| ConstantCondition.cs:152:22:152:22 | Pattern always matches. | Unexpected result: Alert | From 1286420d39910cfbda2f11441d42f610f5940cf7 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 11 Mar 2025 14:42:51 +0100 Subject: [PATCH 6/8] C#: Add change-note. --- csharp/ql/src/change-notes/2025-03-11-constant-condition.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 csharp/ql/src/change-notes/2025-03-11-constant-condition.md diff --git a/csharp/ql/src/change-notes/2025-03-11-constant-condition.md b/csharp/ql/src/change-notes/2025-03-11-constant-condition.md new file mode 100644 index 000000000000..2c9e50136af0 --- /dev/null +++ b/csharp/ql/src/change-notes/2025-03-11-constant-condition.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Increase query precision for `cs/constant-condition` and allow the use of discards in switch/case statements and also take the condition (if any) into account. From 371a72ecec8a44ee18e842ce93cd47025d390830 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 11 Mar 2025 14:54:13 +0100 Subject: [PATCH 7/8] C#: Move Bad test into other file to avoid sync-files breakage. --- .../ConstantCondition/ConstantCondition.expected | 2 +- .../Control-Flow/ConstantCondition/ConstantConditionBad.cs | 7 ------- .../Control-Flow/ConstantCondition/ConstantIfCondition.cs | 5 +++++ 3 files changed, 6 insertions(+), 8 deletions(-) delete mode 100644 csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected index e154d10b9d3a..9e0e69edb904 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected @@ -11,7 +11,6 @@ | 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'. | | ConstantCondition.cs:141:22:141:22 | _ | Pattern always matches. | -| 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'. | | ConstantConditionalExpressionCondition.cs:13:21:13:30 | ... == ... | Condition always evaluates to 'true'. | @@ -20,6 +19,7 @@ | ConstantIfCondition.cs:11:17:11:29 | ... == ... | Condition always evaluates to 'true'. | | ConstantIfCondition.cs:14:17:14:21 | false | Condition always evaluates to 'false'. | | ConstantIfCondition.cs:17:17:17:26 | ... == ... | Condition always evaluates to 'true'. | +| ConstantIfCondition.cs:30:20:30:24 | ... > ... | Condition always evaluates to 'false'. | | ConstantIsNullOrEmpty.cs:10:21:10:54 | call to method IsNullOrEmpty | Condition always evaluates to 'false'. | | ConstantIsNullOrEmpty.cs:46:21:46:46 | call to method IsNullOrEmpty | Condition always evaluates to 'true'. | | ConstantIsNullOrEmpty.cs:50:21:50:44 | call to method IsNullOrEmpty | Condition always evaluates to 'true'. | diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs deleted file mode 100644 index 66af12c057af..000000000000 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionBad.cs +++ /dev/null @@ -1,7 +0,0 @@ -class Bad -{ - public int Max(int a, int b) - { - return a > a ? a : b; // $ Alert - } -} diff --git a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs index 04c91cc222da..146dbcf56611 100644 --- a/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs +++ b/csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs @@ -25,6 +25,11 @@ public void Foo() } } + public int Max(int a, int b) + { + return a > a ? a : b; // $ Alert + } + public int Bar() { return ZERO; From 42f86a823494061b509602c9ae5e55fa24897aae Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Wed, 12 Mar 2025 11:13:25 +0100 Subject: [PATCH 8/8] Update csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql Co-authored-by: Tom Hvitved --- .../Control-Flow/ConstantCondition.ql | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql b/csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql index 6c717bfcb4c1..88d938e399fe 100644 --- a/csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql +++ b/csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql @@ -119,14 +119,14 @@ class ConstantMatchingCondition extends ConstantCondition { } override predicate isWhiteListed() { - exists(Switch se, Case c, int i | c = se.getCase(i) | - c.getPattern() = this.(DiscardExpr) and - ( - i > 0 - or - i = 0 and - exists(Expr cond | c.getCondition() = cond and not isConstantCondition(cond, true)) - ) + exists(Switch se, Case c, int i | + c = se.getCase(i) and + c.getPattern() = this.(DiscardExpr) + | + i > 0 + or + i = 0 and + exists(Expr cond | c.getCondition() = cond and not isConstantCondition(cond, true)) ) or this = any(PositionalPatternExpr ppe).getPattern(_)