Skip to content

Commit daa4532

Browse files
committed
C#: Autoformat QL queries
1 parent 4348de3 commit daa4532

File tree

277 files changed

+2460
-2299
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

277 files changed

+2460
-2299
lines changed

csharp/ql/src/API Abuse/CallToGCCollect.ql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
* @tags efficiency
99
* maintainability
1010
*/
11+
1112
import csharp
1213

1314
from MethodCall c, Method gcCollect
1415
where
15-
c.getTarget() = gcCollect
16-
and gcCollect.hasName("Collect")
17-
and gcCollect.hasNoParameters()
18-
and gcCollect.getDeclaringType().hasQualifiedName("System.GC")
16+
c.getTarget() = gcCollect and
17+
gcCollect.hasName("Collect") and
18+
gcCollect.hasNoParameters() and
19+
gcCollect.getDeclaringType().hasQualifiedName("System.GC")
1920
select c, "Call to 'GC.Collect()'."

csharp/ql/src/API Abuse/CallToObsoleteMethod.ql

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
import csharp
1515

1616
class ObsoleteAttribute extends Attribute {
17-
ObsoleteAttribute() {
18-
this.getType().hasQualifiedName("System", "ObsoleteAttribute")
19-
}
17+
ObsoleteAttribute() { this.getType().hasQualifiedName("System", "ObsoleteAttribute") }
2018
}
2119

2220
from MethodCall c, Method m
23-
where m = c.getTarget()
24-
and m.getAnAttribute() instanceof ObsoleteAttribute
25-
and not c.getEnclosingCallable().(Attributable).getAnAttribute() instanceof ObsoleteAttribute
21+
where
22+
m = c.getTarget() and
23+
m.getAnAttribute() instanceof ObsoleteAttribute and
24+
not c.getEnclosingCallable().(Attributable).getAnAttribute() instanceof ObsoleteAttribute
2625
select c, "Call to obsolete method $@.", m, m.getName()

csharp/ql/src/API Abuse/ClassDoesNotImplementEquals.ql

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,30 @@ import semmle.code.csharp.frameworks.System
1616

1717
from Class c, Element item, string message, string itemText
1818
where
19-
c.isSourceDeclaration()
20-
and not implementsEquals(c)
21-
and not c.isAbstract()
22-
and
19+
c.isSourceDeclaration() and
20+
not implementsEquals(c) and
21+
not c.isAbstract() and
2322
(
2423
exists(MethodCall callToEquals |
25-
callToEquals.getTarget() instanceof EqualsMethod
26-
and callToEquals.getQualifier().getType() = c
27-
and message = "but it is called $@"
28-
and item = callToEquals
29-
and itemText = "here" )
24+
callToEquals.getTarget() instanceof EqualsMethod and
25+
callToEquals.getQualifier().getType() = c and
26+
message = "but it is called $@" and
27+
item = callToEquals and
28+
itemText = "here"
29+
)
3030
or
31-
( item = c.getAnOperator().(EQOperator)
32-
and message = "but it implements $@"
33-
and itemText = "operator ==" )
31+
(
32+
item = c.getAnOperator().(EQOperator) and
33+
message = "but it implements $@" and
34+
itemText = "operator =="
35+
)
3436
or
35-
exists(IEquatableEqualsMethod eq | item = eq
36-
and eq = c.getAMethod()
37-
and message = "but it implements $@"
38-
and itemText = "IEquatable<" + eq.getParameter(0).getType() + ">.Equals"
37+
exists(IEquatableEqualsMethod eq |
38+
item = eq and
39+
eq = c.getAMethod() and
40+
message = "but it implements $@" and
41+
itemText = "IEquatable<" + eq.getParameter(0).getType() + ">.Equals"
3942
)
4043
)
41-
42-
select c, "Class '" + c.getName() + "' does not implement Equals(object), " + message + ".",
43-
item, itemText
44+
select c, "Class '" + c.getName() + "' does not implement Equals(object), " + message + ".", item,
45+
itemText

csharp/ql/src/API Abuse/ClassImplementsICloneable.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import csharp
1414

1515
from ValueOrRefType c
1616
where
17-
c.fromSource()
18-
and c.getABaseInterface+().hasQualifiedName("System", "ICloneable")
19-
and not c.isSealed()
20-
and exists(Method m | m.getDeclaringType() = c and m.hasName("Clone"))
17+
c.fromSource() and
18+
c.getABaseInterface+().hasQualifiedName("System", "ICloneable") and
19+
not c.isSealed() and
20+
exists(Method m | m.getDeclaringType() = c and m.hasName("Clone"))
2121
select c, "Class '" + c.getName() + "' implements 'ICloneable'."

csharp/ql/src/API Abuse/DisposeNotCalledOnException.ql

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ ExceptionClass getAThrownException(Method m) {
2929
or
3030
exists(ControlFlowElement cfe |
3131
cfe = any(ThrowElement te | result = te.getExpr().getType()) or
32-
cfe = any(MethodCall mc | result = getAThrownException(mc.getARuntimeTarget())) |
32+
cfe = any(MethodCall mc | result = getAThrownException(mc.getARuntimeTarget()))
33+
|
3334
cfe.getEnclosingCallable() = m and
3435
not isTriedAgainstException(cfe, result)
3536
)
@@ -39,7 +40,7 @@ ExceptionClass getAThrownException(Method m) {
3940
* Holds if control flow element is tried against throwing an exception of type
4041
* `ec`.
4142
*/
42-
pragma [noinline]
43+
pragma[noinline]
4344
predicate isTriedAgainstException(ControlFlowElement cfe, ExceptionClass ec) {
4445
(cfe instanceof ThrowElement or cfe instanceof MethodCall) and
4546
exists(TryStmt ts |
@@ -53,22 +54,27 @@ predicate isTriedAgainstException(ControlFlowElement cfe, ExceptionClass ec) {
5354
*/
5455
predicate disposeReachableFromDisposableCreation(MethodCall disposeCall, Expr disposableCreation) {
5556
// The qualifier of the Dispose call flows from something that introduced a disposable into scope
56-
(disposableCreation instanceof LocalScopeDisposableCreation or disposableCreation instanceof MethodCall)
57-
and DataFlow::localFlowStep+(DataFlow::exprNode(disposableCreation), DataFlow::exprNode(disposeCall.getQualifier()))
58-
and disposeCall.getTarget() instanceof DisposeMethod
57+
(
58+
disposableCreation instanceof LocalScopeDisposableCreation or
59+
disposableCreation instanceof MethodCall
60+
) and
61+
DataFlow::localFlowStep+(DataFlow::exprNode(disposableCreation),
62+
DataFlow::exprNode(disposeCall.getQualifier())) and
63+
disposeCall.getTarget() instanceof DisposeMethod
5964
}
6065

61-
6266
from MethodCall disposeCall, Expr disposableCreation, MethodCall callThatThrows
6367
where
64-
disposeReachableFromDisposableCreation(disposeCall, disposableCreation)
68+
disposeReachableFromDisposableCreation(disposeCall, disposableCreation) and
6569
// The dispose call is not, itself, within a dispose method.
66-
and not disposeCall.getEnclosingCallable() instanceof DisposeMethod
70+
not disposeCall.getEnclosingCallable() instanceof DisposeMethod and
6771
// Dispose call not within a finally or catch block
68-
and not exists(TryStmt ts |
69-
ts.getACatchClause().getAChild*() = disposeCall or ts.getFinally().getAChild*() = disposeCall)
72+
not exists(TryStmt ts |
73+
ts.getACatchClause().getAChild*() = disposeCall or ts.getFinally().getAChild*() = disposeCall
74+
) and
7075
// At least one method call exists between the allocation and disposal that could throw
71-
and disposableCreation.getAReachableElement() = callThatThrows
72-
and callThatThrows.getAReachableElement() = disposeCall
73-
and exists(getAThrownException(callThatThrows.getARuntimeTarget()))
74-
select disposeCall, "Dispose missed if exception is thrown by $@.", callThatThrows, callThatThrows.toString()
76+
disposableCreation.getAReachableElement() = callThatThrows and
77+
callThatThrows.getAReachableElement() = disposeCall and
78+
exists(getAThrownException(callThatThrows.getARuntimeTarget()))
79+
select disposeCall, "Dispose missed if exception is thrown by $@.", callThatThrows,
80+
callThatThrows.toString()

csharp/ql/src/API Abuse/FormatMissingArgument.ql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import csharp
1313
import semmle.code.csharp.frameworks.Format
1414

1515
from FormatCall format, ValidFormatString src, int used, int supplied
16-
where src = format.getAFormatSource()
17-
and used = src.getAnInsert()
18-
and supplied = format.getSuppliedArguments()
19-
and used >= supplied
16+
where
17+
src = format.getAFormatSource() and
18+
used = src.getAnInsert() and
19+
supplied = format.getSuppliedArguments() and
20+
used >= supplied
2021
select format, "Argument '{" + used + "}' has not been supplied to $@ format string.", src, "this"

csharp/ql/src/API Abuse/FormatUnusedArgument.ql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import csharp
1313
import semmle.code.csharp.frameworks.Format
1414

1515
from FormatCall format, int unused, ValidFormatString src
16-
where src = format.getAFormatSource()
17-
and unused = format.getAnUnusedArgument(src)
18-
select format, "The $@ ignores $@.", src, "format string", format.getSuppliedExpr(unused), "this supplied value"
16+
where
17+
src = format.getAFormatSource() and
18+
unused = format.getAnUnusedArgument(src)
19+
select format, "The $@ ignores $@.", src, "format string", format.getSuppliedExpr(unused),
20+
"this supplied value"

csharp/ql/src/API Abuse/InconsistentEqualsGetHashCode.ql

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,25 @@
1010
* maintainability
1111
* external/cwe/cwe-581
1212
*/
13+
1314
import csharp
1415
import semmle.code.csharp.frameworks.System
1516

1617
from Class c, Method present, string missing
17-
where c.isSourceDeclaration() and
18+
where
19+
c.isSourceDeclaration() and
1820
(
1921
(
20-
present = (EqualsMethod)c.getAMethod() and
22+
present = c.getAMethod().(EqualsMethod) and
2123
not c.getAMethod() instanceof GetHashCodeMethod and
2224
missing = "GetHashCode()"
23-
) or
25+
)
26+
or
2427
(
25-
present = (GetHashCodeMethod)c.getAMethod() and
28+
present = c.getAMethod().(GetHashCodeMethod) and
2629
not implementsEquals(c) and
2730
missing = "Equals(object)"
2831
)
2932
)
30-
select c, "Class '" + c.getName() + "' overrides $@, but not " + missing + ".",
31-
present, present.getName()
33+
select c, "Class '" + c.getName() + "' overrides $@, but not " + missing + ".", present,
34+
present.getName()

csharp/ql/src/API Abuse/IncorrectCompareToSignature.ql

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import csharp
1313
import semmle.code.csharp.frameworks.System
1414

1515
predicate implementsIComparable(ValueOrRefType t, Type paramType) {
16-
exists(ConstructedType ct |
17-
t.getABaseType+() = ct |
16+
exists(ConstructedType ct | t.getABaseType+() = ct |
1817
ct = any(SystemIComparableTInterface i).getAConstructedGeneric() and
1918
paramType = ct.getATypeArgument()
2019
)
@@ -28,20 +27,21 @@ predicate implementsIComparable(ValueOrRefType t, Type paramType) {
2827
}
2928

3029
predicate compareToMethod(Method m, Type paramType) {
31-
m.hasName("CompareTo")
32-
and m.fromSource()
33-
and m.isPublic()
34-
and m.getReturnType() instanceof IntType
35-
and m.getNumberOfParameters() = 1
36-
and paramType = m.getAParameter().getType()
30+
m.hasName("CompareTo") and
31+
m.fromSource() and
32+
m.isPublic() and
33+
m.getReturnType() instanceof IntType and
34+
m.getNumberOfParameters() = 1 and
35+
paramType = m.getAParameter().getType()
3736
}
3837

3938
from Method m, RefType declaringType, Type actualParamType
40-
where m.isSourceDeclaration()
41-
and declaringType = m.getDeclaringType()
42-
and compareToMethod(m, actualParamType)
43-
and not implementsIComparable(declaringType, actualParamType)
44-
select m, "The parameter of this 'CompareTo' method is of type $@, but $@ does not implement 'IComparable<$@>'.",
45-
actualParamType, actualParamType.getName(),
46-
declaringType, declaringType.getName(),
39+
where
40+
m.isSourceDeclaration() and
41+
declaringType = m.getDeclaringType() and
42+
compareToMethod(m, actualParamType) and
43+
not implementsIComparable(declaringType, actualParamType)
44+
select m,
45+
"The parameter of this 'CompareTo' method is of type $@, but $@ does not implement 'IComparable<$@>'.",
46+
actualParamType, actualParamType.getName(), declaringType, declaringType.getName(),
4747
actualParamType, actualParamType.getName()

csharp/ql/src/API Abuse/IncorrectEqualsSignature.ql

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,22 @@ import semmle.code.csharp.frameworks.System
1414

1515
class EqualsOtherMethod extends Method {
1616
EqualsOtherMethod() {
17-
this.hasName("Equals")
18-
and this.getNumberOfParameters() = 1
19-
and this.getReturnType() instanceof BoolType
20-
and this.getDeclaringType() instanceof Class
21-
and not this instanceof EqualsMethod
22-
and not this instanceof IEquatableEqualsMethod
17+
this.hasName("Equals") and
18+
this.getNumberOfParameters() = 1 and
19+
this.getReturnType() instanceof BoolType and
20+
this.getDeclaringType() instanceof Class and
21+
not this instanceof EqualsMethod and
22+
not this instanceof IEquatableEqualsMethod
2323
}
2424

25-
Type getType() {
26-
result = this.getParameter(0).getType()
27-
}
25+
Type getType() { result = this.getParameter(0).getType() }
2826
}
2927

3028
from EqualsOtherMethod equalsOther
31-
where equalsOther.isSourceDeclaration()
32-
and not implementsEquals(equalsOther.getDeclaringType())
33-
select equalsOther, "The $@ of this 'Equals(" + equalsOther.getType().getName() + ")' method does not override 'Equals(object)'.",
34-
equalsOther.getDeclaringType(), "declaring type"
29+
where
30+
equalsOther.isSourceDeclaration() and
31+
not implementsEquals(equalsOther.getDeclaringType())
32+
select equalsOther,
33+
"The $@ of this 'Equals(" + equalsOther.getType().getName() +
34+
")' method does not override 'Equals(object)'.", equalsOther.getDeclaringType(),
35+
"declaring type"

0 commit comments

Comments
 (0)