Skip to content

Commit 31ae6e4

Browse files
committed
C#: Generalize pattern definition to not only include top level pattern definitions.
1 parent 8cae9d0 commit 31ae6e4

4 files changed

Lines changed: 17 additions & 10 deletions

File tree

csharp/ql/lib/semmle/code/csharp/Assignable.qll

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ module AssignableInternal {
321321
TLocalVariableDefinition(LocalVariableDeclExpr lvde) {
322322
not lvde.hasInitializer() and
323323
not exists(getTupleSource(TTupleAssignmentDefinition(_, lvde))) and
324-
not lvde instanceof TopLevelPatternDecl and
324+
not lvde instanceof LocalVariablePatternDecl and
325325
not lvde.isOutArgument()
326326
} or
327327
TImplicitParameterDefinition(Parameter p) {
@@ -340,7 +340,7 @@ module AssignableInternal {
340340
default = p.getDefaultValue()
341341
} or
342342
TAddressOfDefinition(AddressOfExpr aoe) or
343-
TPatternDefinition(TopLevelPatternDecl tlpd) or
343+
TPatternDefinition(LocalVariablePatternDecl lvpd) or
344344
TAssignOperationDefinition(AssignOperation ao) {
345345
ao instanceof AssignCallExpr and not ao instanceof CompoundAssignmentOperatorCall
346346
or
@@ -753,24 +753,31 @@ module AssignableDefinitions {
753753
}
754754

755755
/**
756-
* A local variable definition in a pattern, for example `x is int i`.
756+
* A local variable definition in a pattern, for example `int i` in `x is int i`.
757757
*/
758758
class PatternDefinition extends AssignableDefinition, TPatternDefinition {
759-
TopLevelPatternDecl tlpd;
759+
LocalVariablePatternDecl lvpd;
760760

761-
PatternDefinition() { this = TPatternDefinition(tlpd) }
761+
PatternDefinition() { this = TPatternDefinition(lvpd) }
762762

763763
/** Gets the element matches against this pattern. */
764-
PatternMatch getMatch() { result = tlpd.getMatch() }
764+
PatternMatch getMatch() { result = lvpd.getMatch() }
765765

766766
/** Gets the underlying local variable declaration. */
767-
LocalVariableDeclExpr getDeclaration() { result = tlpd }
767+
LocalVariableDeclExpr getDeclaration() { result = lvpd }
768768

769769
override Expr getSource() { result = this.getMatch().getExpr() }
770770

771771
override string toString() { result = this.getDeclaration().toString() }
772772
}
773773

774+
/**
775+
* A local variable definition at the top level of a pattern.
776+
*/
777+
class TopLevelPatternDefinition extends PatternDefinition {
778+
TopLevelPatternDefinition() { lvpd.isTopLevel() }
779+
}
780+
774781
/**
775782
* An initializer definition for a field or a property, for example
776783
* line 2 in

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ private predicate nonNullDef(SsaExplicitWrite def) {
110110
def.getValue() instanceof NonNullExpr
111111
or
112112
exists(AssignableDefinition ad | ad = def.getDefinition() |
113-
ad instanceof AssignableDefinitions::PatternDefinition
113+
ad instanceof AssignableDefinitions::TopLevelPatternDefinition
114114
or
115115
ad =
116116
any(AssignableDefinitions::LocalVariableDefinition d |

csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2677,7 +2677,7 @@ class CastNode extends Node {
26772677
this.asExpr() instanceof Cast
26782678
or
26792679
this.(AssignableDefinitionNode).getDefinition() instanceof
2680-
AssignableDefinitions::PatternDefinition
2680+
AssignableDefinitions::TopLevelPatternDefinition
26812681
}
26822682
}
26832683

csharp/ql/src/Dead Code/DeadStoreOfLocal.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class RelevantDefinition extends AssignableDefinition {
3535
not lvde.getName() = "_"
3636
)
3737
or
38-
this instanceof AssignableDefinitions::PatternDefinition
38+
this instanceof AssignableDefinitions::TopLevelPatternDefinition
3939
or
4040
this instanceof AssignableDefinitions::AssignOperationDefinition
4141
}

0 commit comments

Comments
 (0)