mirror of
https://github.com/github/codeql.git
synced 2025-12-24 04:36:35 +01:00
C#: Autoformat QL queries
This commit is contained in:
@@ -8,22 +8,20 @@
|
||||
* @tags efficiency
|
||||
* maintainability
|
||||
*/
|
||||
|
||||
import csharp
|
||||
|
||||
// any use of + that has string type
|
||||
class StringCat extends AddExpr {
|
||||
StringCat() {
|
||||
this.getType() instanceof StringType
|
||||
}
|
||||
}
|
||||
class StringCat extends AddExpr { StringCat() { this.getType() instanceof StringType } }
|
||||
|
||||
/*
|
||||
* an assignment of the form
|
||||
* v = ... + ... v ...
|
||||
* where v is a simple variable (and not, for example, a property)
|
||||
*/
|
||||
|
||||
/* an assignment of the form
|
||||
v = ... + ... v ...
|
||||
where v is a simple variable (and not, for example, a property)
|
||||
*/
|
||||
predicate isSelfConcatAssignExpr(AssignExpr e, Variable v) {
|
||||
not e = any(AssignAddExpr a).getExpandedAssignment()
|
||||
and
|
||||
not e = any(AssignAddExpr a).getExpandedAssignment() and
|
||||
exists(VariableAccess use |
|
||||
stringCatContains(e.getRValue(), use) and
|
||||
use.getTarget() = e.getTargetVariable() and
|
||||
@@ -36,20 +34,22 @@ predicate stringCatContains(StringCat expr, Expr child) {
|
||||
stringCatContains(expr, child.getParent())
|
||||
}
|
||||
|
||||
/* an assignment of the form
|
||||
v += ...
|
||||
where v is a simple variable (and not, for example, a property)
|
||||
*/
|
||||
/*
|
||||
* an assignment of the form
|
||||
* v += ...
|
||||
* where v is a simple variable (and not, for example, a property)
|
||||
*/
|
||||
|
||||
predicate isConcatExpr(AssignAddExpr e, Variable v) {
|
||||
e.getLValue().getType() instanceof StringType and
|
||||
v = e.getTargetVariable()
|
||||
}
|
||||
|
||||
|
||||
from Expr e
|
||||
where exists(LoopStmt loop, Variable v |
|
||||
e.getEnclosingStmt().getParent*() = loop and
|
||||
(isSelfConcatAssignExpr(e, v) or isConcatExpr(e, v)) and
|
||||
forall(LocalVariableDeclExpr l | l.getVariable() = v | not l.getParent*() = loop)
|
||||
)
|
||||
where
|
||||
exists(LoopStmt loop, Variable v |
|
||||
e.getEnclosingStmt().getParent*() = loop and
|
||||
(isSelfConcatAssignExpr(e, v) or isConcatExpr(e, v)) and
|
||||
forall(LocalVariableDeclExpr l | l.getVariable() = v | not l.getParent*() = loop)
|
||||
)
|
||||
select e, "String concatenation in loop: use 'StringBuilder'."
|
||||
|
||||
Reference in New Issue
Block a user