mirror of
https://github.com/github/codeql.git
synced 2026-04-18 21:44:02 +02:00
38 lines
1.1 KiB
Plaintext
38 lines
1.1 KiB
Plaintext
/**
|
|
* @name Chain of 'is' tests
|
|
* @description Long sequences of type tests on a variable are generally an indication of questionable design. They are hard to maintain
|
|
* and can cause performance problems.
|
|
* @kind problem
|
|
* @problem.severity recommendation
|
|
* @precision high
|
|
* @id cs/chained-type-tests
|
|
* @tags reliability
|
|
* performance
|
|
* changeability
|
|
* language-features
|
|
*/
|
|
|
|
import csharp
|
|
|
|
TypePatternExpr getTypeCondition(IfStmt is) { result = is.getCondition().(IsExpr).getPattern() }
|
|
|
|
int isCountForIfChain(IfStmt is) {
|
|
exists(int rest |
|
|
(if is.getElse() instanceof IfStmt then rest = isCountForIfChain(is.getElse()) else rest = 0) and
|
|
(
|
|
if getTypeCondition(is).getCheckedType().fromSource()
|
|
then result = 1 + rest
|
|
else result = rest
|
|
)
|
|
)
|
|
}
|
|
|
|
from IfStmt is, int n
|
|
where
|
|
n = isCountForIfChain(is) and
|
|
n > 5 and
|
|
not exists(IfStmt other | is = other.getElse())
|
|
select is,
|
|
"This if block performs a chain of " + n +
|
|
" type tests - consider alternatives, e.g. polymorphism or the visitor pattern."
|