Skip to content

Commit 6b15bf6

Browse files
committed
C#: Rewrite null-coalsecing logic
1 parent 01ad93d commit 6b15bf6

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed

csharp/ql/src/API Abuse/NoDisposeCallOnLocalIDisposable.ql

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,7 @@ private class Conf extends DataFlow::Configuration {
5454
)
5555
or
5656
// A disposing method
57-
exists(Call c, Parameter p |
58-
e = c.getArgumentForParameter(p)
59-
or
60-
// e.g `Stream.Create(input ?? new TextReader())`
61-
exists(NullCoalescingExpr nce |
62-
nce = c.getArgumentForParameter(p) and e = nce.getRightOperand()
63-
)
64-
|
65-
mayBeDisposed(p)
66-
)
57+
exists(Call c, Parameter p | e = c.getArgumentForParameter(p) | mayBeDisposed(p))
6758
or
6859
// Things that are assigned to fields, properties, or indexers may be disposed
6960
exists(AssignableDefinition def, Assignable a |
@@ -90,6 +81,8 @@ private class Conf extends DataFlow::Configuration {
9081

9182
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
9283
node2.asExpr() = any(LocalScopeDisposableCreation other | other.getAnArgument() = node1.asExpr())
84+
or
85+
exists(NullCoalescingExpr nce | node1.asExpr() = nce.getRightOperand() and node2.asExpr() = nce)
9386
}
9487

9588
override predicate isBarrierOut(DataFlow::Node node) {

csharp/ql/test/query-tests/API Abuse/NoDisposeCallOnLocalIDisposable/NoDisposeCallOnLocalIDisposable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public IDisposable Method()
8383
// GOOD: Disposed automatically.
8484
using var c2 = new Timer(TimerProc);
8585

86-
// GOOD: ownership taken via ?? (false positive)
86+
// GOOD: ownership taken via ??
8787
StringReader source = null;
8888
using(XmlReader.Create(source ?? new StringReader("xml"), null))
8989
;

0 commit comments

Comments
 (0)