Skip to content

Commit 92f8a90

Browse files
committed
C#: Introduce a collectionType predicate.
1 parent 8b16c1f commit 92f8a90

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

csharp/ql/lib/semmle/code/csharp/commons/Collections.qll

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/** Provides classes for collections. */
22

33
import csharp
4+
import semmle.code.csharp.frameworks.system.Collections
5+
import semmle.code.csharp.frameworks.system.collections.Generic
46

57
private string modifyMethodName() {
68
result =
@@ -66,15 +68,13 @@ class CollectionType extends RefType {
6668
}
6769
}
6870

69-
/** An IEnumerable type. */
70-
class IEnumerableType extends RefType {
71-
IEnumerableType() {
72-
this.hasQualifiedName("System.Collections", "IEnumerable")
73-
or
74-
this.(ConstructedType)
75-
.getUnboundGeneric()
76-
.hasQualifiedName("System.Collections.Generic", "IEnumerable<>")
77-
}
71+
/** Holds if `t` is a collection type. */
72+
predicate isCollectionType(ValueOrRefType t) {
73+
not t instanceof StringType and
74+
exists(ValueOrRefType base | base = t.getABaseType*() |
75+
base instanceof SystemCollectionsGenericIEnumerableTInterface or
76+
base instanceof SystemCollectionsIEnumerableInterface
77+
)
7878
}
7979

8080
/** An object creation that creates an empty collection. */

csharp/ql/src/utils/model-generator/ModelGeneratorUtilsSpecific.qll

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,9 @@ private predicate isPrimitiveTypeUsedForBulkData(Type t) {
7070
t.getName().regexpMatch("byte|char|Byte|Char")
7171
}
7272

73-
private predicate isContainerType(Type t) {
74-
t instanceof CollectionType or
75-
t instanceof IEnumerableType
76-
}
77-
7873
private string parameterAccess(Parameter p) {
7974
if
80-
isContainerType(p.getType()) and
75+
isCollectionType(p.getType()) and
8176
not isPrimitiveTypeUsedForBulkData(p.getType().(ArrayType).getElementType())
8277
then result = "Argument[" + p.getPosition() + "].Element"
8378
else result = "Argument[" + p.getPosition() + "]"

0 commit comments

Comments
 (0)