-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathAbstractToConcreteCollection.ql
39 lines (36 loc) · 1.33 KB
/
AbstractToConcreteCollection.ql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @name Cast from abstract to concrete collection
* @description A cast from an abstract collection to a concrete implementation type
* makes the code brittle; it is best to program against the abstract
* collection interface only.
* @kind problem
* @problem.severity warning
* @precision medium
* @id cs/cast-from-abstract-to-concrete-collection
* @tags reliability
* maintainability
* modularity
* external/cwe/cwe-485
*/
import csharp
import semmle.code.csharp.frameworks.system.Collections
import semmle.code.csharp.frameworks.system.collections.Generic
/** A collection interface. */
class CollectionInterface extends Interface {
CollectionInterface() {
exists(Interface i | i = this.getABaseInterface*() |
i instanceof SystemCollectionsICollectionInterface or
i.getUnboundDeclaration() instanceof SystemCollectionsGenericICollectionInterface or
i instanceof SystemCollectionsIEnumerableInterface or
i.getUnboundDeclaration() instanceof SystemCollectionsGenericIEnumerableTInterface
)
}
}
from CastExpr e, Class c, CollectionInterface i
where
e.getType() = c and
e.getExpr().getType() = i and
c.isImplicitlyConvertibleTo(i)
select e,
"Questionable cast from abstract '" + i.getName() + "' to concrete implementation '" + c.getName()
+ "'."