Skip to content

Commit e6c258c

Browse files
committed
C#: Restructure Completion::isValidFor()
1 parent 8a35813 commit e6c258c

File tree

1 file changed

+64
-54
lines changed

1 file changed

+64
-54
lines changed

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

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -73,60 +73,70 @@ class Completion extends TCompletion {
7373
else (
7474
this = TThrowCompletion(cfe.(TriedControlFlowElement).getAThrownException())
7575
or
76-
if cfe instanceof ThrowElement
77-
then this = TThrowCompletion(cfe.(ThrowElement).getThrownExceptionType())
78-
else
79-
if mustHaveBooleanCompletion(cfe)
80-
then
81-
exists(boolean value | isBooleanConstant(cfe, value) | this = TBooleanCompletion(value))
82-
or
83-
not isBooleanConstant(cfe, _) and
84-
this = TBooleanCompletion(_)
85-
or
86-
// Corner case: In `if (x ?? y) { ... }`, `x` must have both a `true`
87-
// completion, a `false` completion, and a `null` completion (but not a
88-
// non-`null` completion)
89-
mustHaveNullnessCompletion(cfe) and
90-
this = TNullnessCompletion(true)
91-
else
92-
if mustHaveNullnessCompletion(cfe)
93-
then
94-
exists(boolean value | isNullnessConstant(cfe, value) |
95-
this = TNullnessCompletion(value)
96-
)
97-
or
98-
not isNullnessConstant(cfe, _) and
99-
this = TNullnessCompletion(_)
100-
else
101-
if mustHaveMatchingCompletion(cfe)
102-
then
103-
exists(boolean value | isMatchingConstant(cfe, value) |
104-
this = TMatchingCompletion(value)
105-
)
106-
or
107-
not isMatchingConstant(cfe, _) and
108-
this = TMatchingCompletion(_)
109-
else
110-
if mustHaveEmptinessCompletion(cfe)
111-
then this = TEmptinessCompletion(_)
112-
else
113-
if cfe instanceof BreakStmt
114-
then this = TBreakCompletion()
115-
else
116-
if cfe instanceof ContinueStmt
117-
then this = TContinueCompletion()
118-
else
119-
if cfe instanceof GotoStmt
120-
then this = TGotoCompletion(cfe.(GotoStmt).getLabel())
121-
else
122-
if cfe instanceof ReturnStmt
123-
then this = TReturnCompletion()
124-
else
125-
if cfe instanceof YieldBreakStmt
126-
then
127-
// `yield break` behaves like a return statement
128-
this = TReturnCompletion()
129-
else this = TNormalCompletion()
76+
cfe instanceof ThrowElement and
77+
this = TThrowCompletion(cfe.(ThrowElement).getThrownExceptionType())
78+
or
79+
cfe instanceof BreakStmt and
80+
this = TBreakCompletion()
81+
or
82+
cfe instanceof ContinueStmt and
83+
this = TContinueCompletion()
84+
or
85+
cfe instanceof GotoStmt and
86+
this = TGotoCompletion(cfe.(GotoStmt).getLabel())
87+
or
88+
cfe instanceof ReturnStmt and
89+
this = TReturnCompletion()
90+
or
91+
cfe instanceof YieldBreakStmt and
92+
// `yield break` behaves like a return statement
93+
this = TReturnCompletion()
94+
or
95+
mustHaveBooleanCompletion(cfe) and
96+
(
97+
exists(boolean value | isBooleanConstant(cfe, value) | this = TBooleanCompletion(value))
98+
or
99+
not isBooleanConstant(cfe, _) and
100+
this = TBooleanCompletion(_)
101+
or
102+
// Corner case: In `if (x ?? y) { ... }`, `x` must have both a `true`
103+
// completion, a `false` completion, and a `null` completion (but not a
104+
// non-`null` completion)
105+
mustHaveNullnessCompletion(cfe) and
106+
this = TNullnessCompletion(true)
107+
)
108+
or
109+
mustHaveNullnessCompletion(cfe) and
110+
not mustHaveBooleanCompletion(cfe) and
111+
(
112+
exists(boolean value | isNullnessConstant(cfe, value) | this = TNullnessCompletion(value))
113+
or
114+
not isNullnessConstant(cfe, _) and
115+
this = TNullnessCompletion(_)
116+
)
117+
or
118+
mustHaveMatchingCompletion(cfe) and
119+
(
120+
exists(boolean value | isMatchingConstant(cfe, value) | this = TMatchingCompletion(value))
121+
or
122+
not isMatchingConstant(cfe, _) and
123+
this = TMatchingCompletion(_)
124+
)
125+
or
126+
mustHaveEmptinessCompletion(cfe) and
127+
this = TEmptinessCompletion(_)
128+
or
129+
not cfe instanceof ThrowElement and
130+
not cfe instanceof BreakStmt and
131+
not cfe instanceof ContinueStmt and
132+
not cfe instanceof GotoStmt and
133+
not cfe instanceof ReturnStmt and
134+
not cfe instanceof YieldBreakStmt and
135+
not mustHaveBooleanCompletion(cfe) and
136+
not mustHaveNullnessCompletion(cfe) and
137+
not mustHaveMatchingCompletion(cfe) and
138+
not mustHaveEmptinessCompletion(cfe) and
139+
this = TNormalCompletion()
130140
)
131141
}
132142

0 commit comments

Comments
 (0)