Skip to content

Commit 0d2aa5b

Browse files
committed
unified: Change plugin hook to isNonPattern
1 parent a770173 commit 0d2aa5b

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

unified/ql/lib/codeql/unified/internal/LocalNameBinding.qll

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,20 @@ private module LocalNameBindingInput implements LocalNameBindingInputSig<Locatio
185185
override AstNode getElse() { none() }
186186
}
187187

188+
/** Holds if `e` cannot be a pattern even if it appears in pattern context. */
189+
bindingset[e]
190+
private predicate isNonPattern(Expr e) {
191+
e = any(TypeTestExpr n).getType()
192+
or
193+
e = any(TypeCastExpr n).getType()
194+
or
195+
e instanceof MemberAccessExpr
196+
or
197+
any(NameBindingPlugin p).isNonPattern(e)
198+
}
199+
188200
additional predicate bindingContext(AstNode pattern, AstNode scope, AstNode declaration) {
189-
not any(NameBindingPlugin p).isNameReferenceInPatternContext(pattern) and
201+
not isNonPattern(pattern) and
190202
(
191203
exists(SiblingShadowingDecl decl |
192204
scope = decl and

unified/ql/lib/codeql/unified/internal/NameBindingPlugin.qll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ private import codeql.unified.internal.NameBindingPluginSwift // ensure override
55
/** Extension point for language-specific inputs to name binding. */
66
class NameBindingPlugin extends Unit {
77
/**
8-
* Holds if `identifier`, occurring in pattern context, refers to an existing name.
9-
*
10-
* The caller has already restricted `identifier` to one that appears in pattern context.
8+
* Holds if `e`, occurring in pattern context, should be interpreted a sub-expression
9+
* whose result is to be compared to the incoming value.
1110
*/
12-
bindingset[identifier]
13-
predicate isNameReferenceInPatternContext(Identifier identifier) { none() }
11+
bindingset[e]
12+
predicate isNonPattern(Expr e) { none() }
1413

1514
/**
1615
* Holds if `member` is an instance member.

unified/ql/lib/codeql/unified/internal/NameBindingPluginSwift.qll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ private import unified
66
private import codeql.unified.internal.NameBindingPlugin
77

88
class NameBindingPluginSwift extends NameBindingPlugin {
9-
bindingset[identifier]
10-
override predicate isNameReferenceInPatternContext(Identifier identifier) {
11-
isUnboundPattern(identifier)
12-
}
9+
bindingset[e]
10+
override predicate isNonPattern(Expr e) { isUnboundPattern(e.(Identifier)) }
1311

1412
// Note: For now we assume all code is Swift, but in the future we must restrict these rules to Swift-files
1513
bindingset[cls, member]

0 commit comments

Comments
 (0)