Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
C#: Address some review comments.
Signed-off-by: Vincent Biret <vincentbiret@hotmail.com>
  • Loading branch information
michaelnebel authored and baywet committed Sep 3, 2026
commit 6d867e12e2403b563d943ddd281c78063fa7ec06
24 changes: 12 additions & 12 deletions csharp/ql/lib/Linq/Helpers.qll
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ private int numStmts(ForeachStmt fes) {
else result = 1
}

private predicate returnsLoopVariable(ForeachStmt fes, Stmt s, ReturnStmt ret) {
ret = s.stripSingletonBlocks() and
ret.getExpr().stripCasts().(VariableAccess).getTarget() = fes.getVariable()
private predicate returnsLoopVariable(ForeachStmt fes, Stmt s) {
exists(ReturnStmt ret |
ret = s.stripSingletonBlocks() and
ret.getExpr().stripCasts().(VariableAccess).getTarget() = fes.getVariable()
Comment thread
baywet marked this conversation as resolved.
Outdated
)
}

private predicate hasNullDefault(Type t) { t.isRefType() or t instanceof NullableType }

private predicate returnsDefaultValue(ForeachStmt fes, ReturnStmt ret) {
exists(Type elementType |
private predicate returnsDefaultValueAfterForeach(ForeachStmt fes) {
exists(BlockStmt enclosingBlock, int i, Type elementType, ReturnStmt ret |
enclosingBlock.getStmt(i) = fes and
enclosingBlock.getStmt(i + 1) = ret and
elementType = fes.getVariable().getType()
|
ret.getExpr().stripCasts() instanceof NullLiteral and
Comment thread
baywet marked this conversation as resolved.
Outdated
Expand Down Expand Up @@ -192,18 +196,14 @@ predicate missedFirstOrDefaultOpportunity(ForeachStmtGenericEnumerable fes, IfSt
is = firstStmt(fes) and
not exists(is.getElse()) and
numStmts(fes) = 1 and
// Condition relies on loop variable.
exists(VariableAccess va |
va.getTarget() = fes.getVariable() and
va = is.getCondition().getAChildExpr*()
) and
not is.getCondition().getAChildExpr*() instanceof AwaitExpr and
Comment thread
baywet marked this conversation as resolved.
exists(ReturnStmt defaultRet, BlockStmt enclosingBlock, int i |
returnsLoopVariable(fes, is.getThen(), _) and
// If no element matches, the method returns the same value that FirstOrDefault would.
returnsDefaultValue(fes, defaultRet) and
enclosingBlock.getStmt(i) = fes and
enclosingBlock.getStmt(i + 1) = defaultRet
)
returnsLoopVariable(fes, is.getThen()) and
returnsDefaultValueAfterForeach(fes)
Comment thread
baywet marked this conversation as resolved.
}

//#################### CLASSES ####################
Expand Down
Loading