Skip to content

Commit 48d0ee3

Browse files
committed
C#: Use the NullCoalescingOperation class where needed (now that assignments are no longer expanded).
1 parent 57c6efc commit 48d0ee3

File tree

8 files changed

+21
-29
lines changed

8 files changed

+21
-29
lines changed

csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,7 @@ private module LogicInput implements GuardsImpl::LogicInputSig {
292292
v1.isNonNullValue() and
293293
v2 = v1
294294
or
295-
g2 = g1.(NullCoalescingExpr).getAnOperand() and
296-
v1.isNullValue() and
297-
v2 = v1
298-
or
299-
g2 = g1.(AssignCoalesceExpr).getAnOperand() and
295+
g2 = g1.(NullCoalescingOperation).getAnOperand() and
300296
v1.isNullValue() and
301297
v2 = v1
302298
or
@@ -844,18 +840,14 @@ module Internal {
844840
or
845841
e1 = e2.(Cast).getExpr()
846842
or
847-
e2 = e1.(NullCoalescingExpr).getAnOperand()
848-
or
849-
e2 = e1.(AssignCoalesceExpr).getAnOperand()
843+
e2 = e1.(NullCoalescingOperation).getAnOperand()
850844
}
851845

852846
/** Holds if expression `e3` is a `null` value whenever `e1` and `e2` are. */
853847
predicate nullValueImpliedBinary(Expr e1, Expr e2, Expr e3) {
854848
e3 = any(ConditionalExpr ce | e1 = ce.getThen() and e2 = ce.getElse())
855849
or
856-
e3 = any(NullCoalescingExpr nce | e1 = nce.getLeftOperand() and e2 = nce.getRightOperand())
857-
or
858-
e3 = any(AssignCoalesceExpr ace | e1 = ace.getLeftOperand() and e2 = ace.getRightOperand())
850+
e3 = any(NullCoalescingOperation no | e1 = no.getLeftOperand() and e2 = no.getRightOperand())
859851
}
860852

861853
predicate nullValueImplied(Expr e) {
@@ -932,8 +924,7 @@ module Internal {
932924
predicate nonNullValueImpliedUnary(Expr e1, Expr e2) {
933925
e1 = e2.(CastExpr).getExpr() or
934926
e1 = e2.(AssignExpr).getRValue() or
935-
e1 = e2.(NullCoalescingExpr).getAnOperand() or
936-
e1 = e2.(AssignCoalesceExpr).getAnOperand()
927+
e1 = e2.(NullCoalescingOperation).getAnOperand()
937928
}
938929

939930
/**

csharp/ql/lib/semmle/code/csharp/controlflow/internal/Completion.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ private predicate inBooleanContext(Expr e) {
447447
e in [ce.getThen(), ce.getElse()]
448448
)
449449
or
450-
e = any(NullCoalescingExpr nce | inBooleanContext(nce)).getAnOperand()
450+
e = any(NullCoalescingOperation nce | inBooleanContext(nce)).getAnOperand()
451451
or
452452
e = any(SwitchExpr se | inBooleanContext(se)).getACase()
453453
or
@@ -467,13 +467,13 @@ private predicate mustHaveNullnessCompletion(Expr e) {
467467
* that `e` evaluates to determines a `null`/non-`null` branch successor.
468468
*/
469469
private predicate inNullnessContext(Expr e) {
470-
e = any(NullCoalescingExpr nce).getLeftOperand()
470+
e = any(NullCoalescingOperation nce).getLeftOperand()
471471
or
472472
exists(QualifiableExpr qe | qe.isConditional() | e = qe.getChildExpr(-1))
473473
or
474474
exists(ConditionalExpr ce | inNullnessContext(ce) | (e = ce.getThen() or e = ce.getElse()))
475475
or
476-
exists(NullCoalescingExpr nce | inNullnessContext(nce) | e = nce.getRightOperand())
476+
exists(NullCoalescingOperation nce | inNullnessContext(nce) | e = nce.getRightOperand())
477477
or
478478
e = any(SwitchExpr se | inNullnessContext(se)).getACase()
479479
or

csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ module Expressions {
504504
not this instanceof LogicalNotExpr and
505505
not this instanceof LogicalAndExpr and
506506
not this instanceof LogicalOrExpr and
507-
not this instanceof NullCoalescingExpr and
507+
not this instanceof NullCoalescingOperation and
508508
not this instanceof ConditionalExpr and
509509
not this instanceof ConditionallyQualifiedExpr and
510510
not this instanceof ThrowExpr and
@@ -734,7 +734,8 @@ module Expressions {
734734
}
735735
}
736736

737-
private class NullCoalescingExprTree extends PostOrderTree instanceof NullCoalescingExpr {
737+
private class NullCoalescingOperationTree extends PostOrderTree instanceof NullCoalescingOperation
738+
{
738739
final override predicate propagatesAbnormal(AstNode child) {
739740
child in [super.getLeftOperand(), super.getRightOperand()]
740741
}

csharp/ql/lib/semmle/code/csharp/controlflow/internal/Splitting.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module ConditionalCompletionSplitting {
9595
child = parent.(SwitchCaseExpr).getBody()
9696
or
9797
parent =
98-
any(NullCoalescingExpr nce |
98+
any(NullCoalescingOperation nce |
9999
if childCompletion instanceof NullnessCompletion
100100
then child = nce.getRightOperand()
101101
else child = nce.getAnOperand()

csharp/ql/lib/semmle/code/csharp/dataflow/Nullness.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private Expr maybeNullExpr(Expr reason) {
4242
ce.getElse() = maybeNullExpr(reason)
4343
)
4444
or
45-
result.(NullCoalescingExpr).getRightOperand() = maybeNullExpr(reason)
45+
result.(NullCoalescingOperation).getRightOperand() = maybeNullExpr(reason)
4646
or
4747
result =
4848
any(QualifiableExpr qe |

csharp/ql/lib/semmle/code/csharp/metrics/Complexity.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private predicate branchingExpr(Expr expr) {
3232
expr instanceof ConditionalExpr or
3333
expr instanceof LogicalAndExpr or
3434
expr instanceof LogicalOrExpr or
35-
expr instanceof NullCoalescingExpr
35+
expr instanceof NullCoalescingOperation
3636
}
3737

3838
/**

csharp/ql/src/Bad Practices/Control-Flow/ConstantCondition.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module ConstCondImpl = ConstCond::Make<Location, Cfg, ConstCondInput>;
4343
predicate nullCheck(Expr e, boolean direct) {
4444
exists(QualifiableExpr qe | qe.isConditional() and qe.getQualifier() = e and direct = true)
4545
or
46-
exists(NullCoalescingExpr nce | nce.getLeftOperand() = e and direct = true)
46+
exists(NullCoalescingOperation nce | nce.getLeftOperand() = e and direct = true)
4747
or
4848
exists(ConditionalExpr ce | ce.getThen() = e or ce.getElse() = e |
4949
nullCheck(ce, _) and direct = false
@@ -114,7 +114,7 @@ class ConstantBooleanCondition extends ConstantCondition {
114114

115115
override predicate isWhiteListed() {
116116
// E.g. `x ?? false`
117-
this.(BoolLiteral) = any(NullCoalescingExpr nce).getRightOperand() or
117+
this.(BoolLiteral) = any(NullCoalescingOperation nce).getRightOperand() or
118118
// No need to flag logical operations when the operands are constant
119119
isConstantCondition(this.(LogicalNotExpr).getOperand(), _) or
120120
this =

csharp/ql/src/Language Abuse/UselessNullCoalescingExpression.ql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ import semmle.code.csharp.commons.StructuralComparison
1717

1818
pragma[noinline]
1919
private predicate same(AssignableAccess x, AssignableAccess y) {
20-
exists(NullCoalescingExpr nce |
21-
x = nce.getLeftOperand() and
22-
y = nce.getRightOperand().getAChildExpr*()
20+
exists(NullCoalescingOperation nc |
21+
x = nc.getLeftOperand() and
22+
y = nc.getRightOperand().getAChildExpr*()
2323
) and
2424
sameGvn(x, y)
2525
}
2626

27-
private predicate uselessNullCoalescingExpr(NullCoalescingExpr nce) {
27+
private predicate uselessNullCoalescingOperation(NullCoalescingOperation nce) {
2828
exists(AssignableAccess x |
2929
nce.getLeftOperand() = x and
3030
forex(AssignableAccess y | same(x, y) | y instanceof AssignableRead and not y.isRefArgument())
3131
)
3232
}
3333

34-
from NullCoalescingExpr nce
35-
where uselessNullCoalescingExpr(nce)
34+
from NullCoalescingOperation nce
35+
where uselessNullCoalescingOperation(nce)
3636
select nce, "Both operands of this null-coalescing expression access the same variable or property."

0 commit comments

Comments
 (0)