C#: Simplify the ConstantCondition query.#21599
Conversation
e30a08f to
2d39a2b
Compare
|
I've tested the constant-type-check part of the query on MRVA - there were very few results, and those that I checked were FPs, so dropping that seems perfectly fine. |
92225e6 to
371b9b8
Compare
|
QHelp previews: |
371b9b8 to
86cc188
Compare
There was a problem hiding this comment.
Pull request overview
Simplifies the C# cs/constant-condition query implementation ahead of an upcoming CFG change by removing reliance on the CFG “round-trip”, narrowing constant-boolean reporting to constant comparisons, and dropping constant pattern-matching reporting; as a consequence, the standalone cs/constant-comparison query becomes redundant and is removed.
Changes:
- Reworks
cs/constant-conditionto use constant-comparison/nullness logic directly and removes constant-matching and literal true/false condition reporting. - Removes the
cs/constant-comparisonquery from the repo and from the standard suites/tests. - Updates/reshapes query-tests and expected outputs to match the new alert surface.
Reviewed changes
Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected | Updates standalone expected results to reflect removed constant-matching reporting. |
| csharp/ql/test/query-tests/standalone/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs | Updates inline expectation comment to document descoped behavior. |
| csharp/ql/test/query-tests/Likely Bugs/ConstantComparison/options | Removes test harness configuration for the deleted ConstantComparison query. |
| csharp/ql/test/query-tests/Likely Bugs/ConstantComparison/ConstantComparison.qlref | Removes qlref for the deleted ConstantComparison query. |
| csharp/ql/test/query-tests/Likely Bugs/ConstantComparison/ConstantComparison.expected | Removes expected results for the deleted ConstantComparison query. |
| csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantWhileCondition.cs | Adjusts test annotations to reflect no longer flagging literal false loop conditions. |
| csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIsNullOrEmpty.cs | Adjusts test annotations around IsNullOrEmpty constant cases. |
| csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIfCondition.cs | Adjusts test annotations and adds an unsigned-range constant comparison case. |
| csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantForCondition.cs | Adjusts test annotations for literal false loop conditions. |
| csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantDoCondition.cs | Updates annotations to expect alerts for constant comparison do/while conditions. |
| csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantConditionalExpressionCondition.cs | Adjusts conditional-expression annotations consistent with the new scope. |
| csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.expected | Updates expected results to include constant-comparison-derived findings and remove descoped ones. |
| csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs | Updates pattern-matching test annotations and keeps other cases aligned with new query behavior. |
| csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantComparison.cs | Retargets constant-comparison test cases to be consumed by ConstantCondition. |
| csharp/ql/src/Likely Bugs/ConstantComparison.ql | Deletes the now-redundant ConstantComparison query. |
| csharp/ql/src/Likely Bugs/ConstantComparison.qhelp | Deletes documentation for the removed ConstantComparison query. |
| csharp/ql/src/Likely Bugs/ConstantComparison.cs | Deletes the sample source for the removed ConstantComparison query. |
| csharp/ql/src/CSI/CompareIdenticalValues.ql | Updates overlap-avoidance logic now that ConstantComparison is removed/re-scoped. |
| csharp/ql/src/codeql-suites/csharp-security-and-quality.qls | Removes cs/constant-comparison from the standard suite. |
| csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql | Refactors ConstantCondition implementation: direct nullness checks, comparisons-only boolean conditions, drops constant matching. |
| csharp/ql/lib/semmle/code/csharp/commons/Constants.qll | Removes isConstantCondition predicate (no longer used). |
| csharp/ql/integration-tests/posix/query-suite/csharp-security-and-quality.qls.expected | Updates integration expected suite contents after query removal. |
| csharp/ql/integration-tests/posix/query-suite/csharp-code-quality.qls.expected | Updates integration expected suite contents after query removal. |
| csharp/ql/integration-tests/posix/query-suite/csharp-code-quality-extended.qls.expected | Updates integration expected suite contents after query removal. |
Comments suppressed due to low confidence (3)
csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs:76
case int _:is annotated as// Missing Alert, but constant pattern/matching reporting is being removed in this PR. Consider marking this as// Descoped/// GOODto match the new intended behavior rather than implying a bug.
switch ((object)s)
{
case int _: // Missing Alert
break;
csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIsNullOrEmpty.cs:16
- The
// Missing Alertannotations forstring.IsNullOrEmpty(...)calls look intentional given the PR’s decision to stop special-casingIsNullOrEmptyon constants. Consider relabeling them as// Descoped(or similar) so the test doesn’t imply the query is unexpectedly failing.
This issue also appears on line 46 of the same file.
if (string.IsNullOrEmpty(nameof(args))) // Missing Alert (always false)
{
}
string? x = null;
if (string.IsNullOrEmpty(x)) // Missing Alert (always true)
{
csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantIsNullOrEmpty.cs:54
- Same as earlier in this file: these
// Missing Alertnotes appear to reflect intentional descoping of constantIsNullOrEmptyhandling. Consider renaming to// Descoped/// GOODfor clarity.
if (string.IsNullOrEmpty(null)) // Missing Alert
{
}
if (string.IsNullOrEmpty("")) // Missing Alert
{
}
if (string.IsNullOrEmpty(" ")) // Missing Alert
csharp/ql/test/query-tests/Bad Practices/Control-Flow/ConstantCondition/ConstantCondition.cs
Outdated
Show resolved
Hide resolved
86cc188 to
29500c7
Compare
| stripped.getType() = | ||
| any(ValueType t | | ||
| not t instanceof NullableType and | ||
| // Extractor bug: the type of `x?.Length` is reported as `int`, but it should | ||
| // be `int?` | ||
| not getQualifier*(stripped).(QualifiableExpr).isConditional() | ||
| ) and | ||
| b = false | ||
| or | ||
| stripped instanceof NullLiteral and | ||
| b = true | ||
| or | ||
| stripped.hasValue() and | ||
| not stripped instanceof NullLiteral and | ||
| b = false |
There was a problem hiding this comment.
Would it be possible to introduce a helper predicate for this and re-use it in Completion.qll as well?
There was a problem hiding this comment.
That's scheduled for deletion in an upcoming PR.
michaelnebel
left a comment
There was a problem hiding this comment.
Looks good to me!
One tiny nitpick related to code-sharing.
Several simplifications are made (motivated by the upcoming CFG switch):
isNullOrEmptycalled on constants that happens inside the CFG construction seems a bit too narrow to warrant attention, and it's not something that'll be preserved in the new CFG. This allows us to get rid of all the allow-listing that was due to the the CFG round-trip.The ConstantCondition query was partially implemented inside the CFG (in
Completion.qll). In particular, this included all ofisConstantComparisonfromConstants.qll, which was the main implementation for the ConstantComparison query. But due to the CFG round-trip the ConstantCondition query only kept those results that were manifested as missing conditional CFG edges. Short-circuiting this round-trip means that the ConstantComparison query is made redundant.