C#: Introduce a collectionType predicate.

This commit is contained in:
Michael Nebel
2022-03-21 14:44:38 +01:00
parent 8b16c1f585
commit 92f8a90f31
2 changed files with 10 additions and 15 deletions

View File

@@ -1,6 +1,8 @@
/** Provides classes for collections. */
import csharp
import semmle.code.csharp.frameworks.system.Collections
import semmle.code.csharp.frameworks.system.collections.Generic
private string modifyMethodName() {
result =
@@ -66,15 +68,13 @@ class CollectionType extends RefType {
}
}
/** An IEnumerable type. */
class IEnumerableType extends RefType {
IEnumerableType() {
this.hasQualifiedName("System.Collections", "IEnumerable")
or
this.(ConstructedType)
.getUnboundGeneric()
.hasQualifiedName("System.Collections.Generic", "IEnumerable<>")
}
/** Holds if `t` is a collection type. */
predicate isCollectionType(ValueOrRefType t) {
not t instanceof StringType and
exists(ValueOrRefType base | base = t.getABaseType*() |
base instanceof SystemCollectionsGenericIEnumerableTInterface or
base instanceof SystemCollectionsIEnumerableInterface
)
}
/** An object creation that creates an empty collection. */

View File

@@ -70,14 +70,9 @@ private predicate isPrimitiveTypeUsedForBulkData(Type t) {
t.getName().regexpMatch("byte|char|Byte|Char")
}
private predicate isContainerType(Type t) {
t instanceof CollectionType or
t instanceof IEnumerableType
}
private string parameterAccess(Parameter p) {
if
isContainerType(p.getType()) and
isCollectionType(p.getType()) and
not isPrimitiveTypeUsedForBulkData(p.getType().(ArrayType).getElementType())
then result = "Argument[" + p.getPosition() + "].Element"
else result = "Argument[" + p.getPosition() + "]"