mirror of
https://github.com/github/codeql.git
synced 2026-04-30 11:15:13 +02:00
C#: Autoformat QL queries
This commit is contained in:
@@ -9,45 +9,43 @@
|
||||
* useless-code
|
||||
* external/cwe/cwe-561
|
||||
*/
|
||||
|
||||
import csharp
|
||||
import semmle.code.csharp.commons.Util
|
||||
import semmle.code.csharp.frameworks.Test
|
||||
import semmle.code.csharp.metrics.Coupling
|
||||
|
||||
predicate potentiallyUsedFromXaml(RefType t)
|
||||
{
|
||||
predicate potentiallyUsedFromXaml(RefType t) {
|
||||
exists(string name | name = t.getABaseType*().getQualifiedName() |
|
||||
name = "System.Windows.Data.IValueConverter"
|
||||
or name = "System.Windows.Data.IMultiValueConverter"
|
||||
name = "System.Windows.Data.IValueConverter" or
|
||||
name = "System.Windows.Data.IMultiValueConverter"
|
||||
)
|
||||
}
|
||||
|
||||
class ExportAttribute extends Attribute
|
||||
{
|
||||
ExportAttribute()
|
||||
{
|
||||
class ExportAttribute extends Attribute {
|
||||
ExportAttribute() {
|
||||
getType().hasQualifiedName("System.ComponentModel.Composition.ExportAttribute")
|
||||
}
|
||||
}
|
||||
|
||||
from RefType t
|
||||
where
|
||||
not extractionIsStandalone()
|
||||
and t.fromSource()
|
||||
and t = t.getSourceDeclaration()
|
||||
and not t instanceof AnonymousClass
|
||||
and not (t.isPublic() or t.isProtected())
|
||||
and not exists (ValueOrRefType dependent | depends(dependent, t) and dependent != t)
|
||||
and not exists (ConstructedType ct | usesType(ct, t))
|
||||
and not exists (MethodCall call | usesType(call.getTarget().(ConstructedMethod).getATypeArgument(), t))
|
||||
and not t.getAMethod() instanceof MainMethod
|
||||
and not potentiallyUsedFromXaml(t)
|
||||
and not exists(TypeofExpr typeof | typeof.getTypeAccess().getTarget() = t)
|
||||
and not t instanceof TestClass
|
||||
not extractionIsStandalone() and
|
||||
t.fromSource() and
|
||||
t = t.getSourceDeclaration() and
|
||||
not t instanceof AnonymousClass and
|
||||
not (t.isPublic() or t.isProtected()) and
|
||||
not exists(ValueOrRefType dependent | depends(dependent, t) and dependent != t) and
|
||||
not exists(ConstructedType ct | usesType(ct, t)) and
|
||||
not exists(MethodCall call | usesType(call.getTarget().(ConstructedMethod).getATypeArgument(), t)) and
|
||||
not t.getAMethod() instanceof MainMethod and
|
||||
not potentiallyUsedFromXaml(t) and
|
||||
not exists(TypeofExpr typeof | typeof.getTypeAccess().getTarget() = t) and
|
||||
not t instanceof TestClass and
|
||||
// MemberConstant nodes are compile-time constant and can appear in various contexts
|
||||
// where they don't have enclosing callables or types (e.g. in attribute values).
|
||||
// Classes that are declared purely to hold member constants which are used are,
|
||||
// therefore, not dead.
|
||||
and not exists(t.getAMember().(MemberConstant).getAnAccess())
|
||||
and not t.getAnAttribute() instanceof ExportAttribute
|
||||
not exists(t.getAMember().(MemberConstant).getAnAccess()) and
|
||||
not t.getAnAttribute() instanceof ExportAttribute
|
||||
select t, "Unused reference type " + t + "."
|
||||
|
||||
@@ -20,8 +20,7 @@ import csharp
|
||||
Callable getACapturingCallableAncestor(LocalVariable v) {
|
||||
result = v.getACapturingCallable()
|
||||
or
|
||||
exists(Callable mid |
|
||||
mid = getACapturingCallableAncestor(v) |
|
||||
exists(Callable mid | mid = getACapturingCallableAncestor(v) |
|
||||
result = mid.getEnclosingCallable() and
|
||||
not v.getEnclosingCallable() = result
|
||||
)
|
||||
@@ -37,8 +36,7 @@ Expr getADelegateExpr(Callable c) {
|
||||
* Holds if `c` is a call where any delegate argument is evaluated immediately.
|
||||
*/
|
||||
predicate nonEscapingCall(Call c) {
|
||||
exists(string name |
|
||||
c.getTarget().hasName(name) |
|
||||
exists(string name | c.getTarget().hasName(name) |
|
||||
name = "ForEach" or
|
||||
name = "Count" or
|
||||
name = "Any" or
|
||||
@@ -62,8 +60,7 @@ predicate nonEscapingCall(Call c) {
|
||||
* `v` may escape the local scope.
|
||||
*/
|
||||
predicate mayEscape(LocalVariable v) {
|
||||
exists(Callable c, Expr e, Expr succ |
|
||||
c = getACapturingCallableAncestor(v) |
|
||||
exists(Callable c, Expr e, Expr succ | c = getACapturingCallableAncestor(v) |
|
||||
e = getADelegateExpr(c) and
|
||||
DataFlow::localFlow(DataFlow::exprNode(e), DataFlow::exprNode(succ)) and
|
||||
not succ = any(DelegateCall dc).getDelegateExpr() and
|
||||
@@ -80,15 +77,15 @@ class RelevantDefinition extends AssignableDefinition {
|
||||
this instanceof AssignableDefinitions::MutationDefinition
|
||||
or
|
||||
this instanceof AssignableDefinitions::TupleAssignmentDefinition
|
||||
or
|
||||
// Discards in out assignments are only possible from C# 7 (2017), so we disable this case
|
||||
// for now
|
||||
//or
|
||||
//this.(AssignableDefinitions::OutRefDefinition).getTargetAccess().isOutArgument()
|
||||
or
|
||||
this.(AssignableDefinitions::LocalVariableDefinition).getDeclaration() = any(LocalVariableDeclExpr lvde |
|
||||
lvde = any(SpecificCatchClause scc).getVariableDeclExpr() or
|
||||
lvde = any(ForeachStmt fs).getVariableDeclExpr()
|
||||
)
|
||||
lvde = any(SpecificCatchClause scc).getVariableDeclExpr() or
|
||||
lvde = any(ForeachStmt fs).getVariableDeclExpr()
|
||||
)
|
||||
or
|
||||
this instanceof AssignableDefinitions::IsPatternDefinition
|
||||
or
|
||||
@@ -97,8 +94,7 @@ class RelevantDefinition extends AssignableDefinition {
|
||||
|
||||
/** Holds if this assignment may be live. */
|
||||
private predicate isMaybeLive() {
|
||||
exists(LocalVariable v |
|
||||
v = this.getTarget() |
|
||||
exists(LocalVariable v | v = this.getTarget() |
|
||||
// SSA definitions are only created for live variables
|
||||
this = any(Ssa::ExplicitDefinition ssaDef).getADefinition()
|
||||
or
|
||||
@@ -117,10 +113,8 @@ class RelevantDefinition extends AssignableDefinition {
|
||||
*/
|
||||
private predicate isDefaultLikeInitializer() {
|
||||
this.isInitializer() and
|
||||
exists(Expr e |
|
||||
e = this.getSource() |
|
||||
exists(string val |
|
||||
val = e.getValue() |
|
||||
exists(Expr e | e = this.getSource() |
|
||||
exists(string val | val = e.getValue() |
|
||||
val = "0" or
|
||||
val = "-1" or
|
||||
val = "" or
|
||||
@@ -129,7 +123,10 @@ class RelevantDefinition extends AssignableDefinition {
|
||||
or
|
||||
e instanceof NullLiteral
|
||||
or
|
||||
e = any(Field f | f.isStatic() and (f.isReadOnly() or f.isConst())).getAnAccess()
|
||||
e = any(Field f |
|
||||
f.isStatic() and
|
||||
(f.isReadOnly() or f.isConst())
|
||||
).getAnAccess()
|
||||
or
|
||||
e instanceof DefaultValueExpr
|
||||
or
|
||||
@@ -151,8 +148,7 @@ class RelevantDefinition extends AssignableDefinition {
|
||||
(
|
||||
not this.isDefaultLikeInitializer()
|
||||
or
|
||||
not exists(AssignableDefinition other |
|
||||
other.getTarget() = this.getTarget() |
|
||||
not exists(AssignableDefinition other | other.getTarget() = this.getTarget() |
|
||||
other != this
|
||||
)
|
||||
)
|
||||
@@ -161,6 +157,7 @@ class RelevantDefinition extends AssignableDefinition {
|
||||
}
|
||||
|
||||
from RelevantDefinition def, LocalVariable v
|
||||
where v = def.getTarget()
|
||||
and def.isDead()
|
||||
where
|
||||
v = def.getTarget() and
|
||||
def.isDead()
|
||||
select def, "This assignment to $@ is useless, since its value is never read.", v, v.getName()
|
||||
|
||||
@@ -16,108 +16,102 @@ import semmle.code.csharp.frameworks.System
|
||||
import semmle.code.csharp.frameworks.system.runtime.InteropServices
|
||||
|
||||
// Any field transitively contained in t.
|
||||
Field getANestedField(ValueOrRefType t)
|
||||
{
|
||||
Field getANestedField(ValueOrRefType t) {
|
||||
result.getDeclaringType() = t
|
||||
or
|
||||
exists(Field mid |
|
||||
mid=getANestedField(t)
|
||||
and
|
||||
mid.getType() = result.getDeclaringType())
|
||||
mid = getANestedField(t) and
|
||||
mid.getType() = result.getDeclaringType()
|
||||
)
|
||||
}
|
||||
|
||||
// Any ValueOrRefType referenced by a Type, including TypeParameters.
|
||||
ValueOrRefType getAReferencedType(Type t)
|
||||
{
|
||||
ValueOrRefType getAReferencedType(Type t) {
|
||||
result = t
|
||||
or
|
||||
result = t.(TypeParameter).getASuppliedType()
|
||||
}
|
||||
|
||||
predicate isTypeExternallyInitialized(ValueOrRefType t)
|
||||
{
|
||||
predicate isTypeExternallyInitialized(ValueOrRefType t) {
|
||||
// The type got created via a call to PtrToStructure().
|
||||
exists(MethodCall mc |
|
||||
mc.getTarget() = any(SystemRuntimeInteropServicesMarshalClass c).getPtrToStructureTypeMethod()
|
||||
and
|
||||
mc.getTarget() = any(SystemRuntimeInteropServicesMarshalClass c).getPtrToStructureTypeMethod() and
|
||||
t = getAReferencedType(mc.getArgument(1).(TypeofExpr).getTypeAccess().getTarget())
|
||||
)
|
||||
|
||||
)
|
||||
or
|
||||
// The type got created via a call to PtrToStructure().
|
||||
or exists(MethodCall mc |
|
||||
mc.getTarget() = any(SystemRuntimeInteropServicesMarshalClass c).getPtrToStructureObjectMethod()
|
||||
and
|
||||
exists(MethodCall mc |
|
||||
mc.getTarget() = any(SystemRuntimeInteropServicesMarshalClass c).getPtrToStructureObjectMethod() and
|
||||
t = getAReferencedType(mc.getArgument(1).getType())
|
||||
)
|
||||
|
||||
)
|
||||
or
|
||||
// An extern method exists which could initialize the type.
|
||||
or exists(Method m, Parameter p |
|
||||
exists(Method m, Parameter p |
|
||||
isExternMethod(m) and
|
||||
p=m.getAParameter() and
|
||||
p = m.getAParameter() and
|
||||
t = p.getType()
|
||||
|
|
||||
p.isOut() or p.isRef())
|
||||
|
||||
|
|
||||
p.isOut() or p.isRef()
|
||||
)
|
||||
or
|
||||
// The data structure has been cast to a pointer - all bets are off.
|
||||
or exists(CastExpr c |
|
||||
t = getAReferencedType(c.getTargetType().(PointerType).getReferentType()))
|
||||
exists(CastExpr c | t = getAReferencedType(c.getTargetType().(PointerType).getReferentType()))
|
||||
}
|
||||
|
||||
// The type is potentially marshaled using an extern or interop.
|
||||
predicate isFieldExternallyInitialized(Field f)
|
||||
{
|
||||
predicate isFieldExternallyInitialized(Field f) {
|
||||
exists(ValueOrRefType t |
|
||||
f = getANestedField(t)
|
||||
and
|
||||
isTypeExternallyInitialized(t))
|
||||
f = getANestedField(t) and
|
||||
isTypeExternallyInitialized(t)
|
||||
)
|
||||
}
|
||||
|
||||
predicate isExternMethod(Method externMethod)
|
||||
{
|
||||
predicate isExternMethod(Method externMethod) {
|
||||
externMethod.isExtern()
|
||||
or
|
||||
externMethod.getAnAttribute().getType() instanceof SystemRuntimeInteropServicesDllImportAttributeClass
|
||||
externMethod.getAnAttribute().getType() instanceof
|
||||
SystemRuntimeInteropServicesDllImportAttributeClass
|
||||
or
|
||||
externMethod.getDeclaringType().getAnAttribute().getType() instanceof SystemRuntimeInteropServicesComImportAttributeClass
|
||||
externMethod.getDeclaringType().getAnAttribute().getType() instanceof
|
||||
SystemRuntimeInteropServicesComImportAttributeClass
|
||||
}
|
||||
|
||||
from Field f, FieldRead fa
|
||||
where
|
||||
f.fromSource()
|
||||
and not extractionIsStandalone()
|
||||
and not f.isReadOnly()
|
||||
and not f.isConst()
|
||||
and not f.getDeclaringType() instanceof Enum
|
||||
and not f.getType() instanceof Struct
|
||||
and not exists(Assignment ae, Field g |
|
||||
ae.getLValue().(FieldAccess).getTarget() = g
|
||||
and g.getSourceDeclaration() = f
|
||||
and not (ae.getRValue() instanceof NullLiteral)
|
||||
)
|
||||
and not exists(MethodCall mc, int i, Field g |
|
||||
exists(Parameter p | mc.getTarget().getParameter(i) = p |
|
||||
p.isOut() or p.isRef()
|
||||
)
|
||||
and mc.getArgument(i) = g.getAnAccess()
|
||||
and g.getSourceDeclaration() = f
|
||||
)
|
||||
and not isFieldExternallyInitialized(f)
|
||||
and not exists(f.getAnAttribute())
|
||||
and not exists(Expr init, Field g |
|
||||
f.fromSource() and
|
||||
not extractionIsStandalone() and
|
||||
not f.isReadOnly() and
|
||||
not f.isConst() and
|
||||
not f.getDeclaringType() instanceof Enum and
|
||||
not f.getType() instanceof Struct and
|
||||
not exists(Assignment ae, Field g |
|
||||
ae.getLValue().(FieldAccess).getTarget() = g and
|
||||
g.getSourceDeclaration() = f and
|
||||
not (ae.getRValue() instanceof NullLiteral)
|
||||
) and
|
||||
not exists(MethodCall mc, int i, Field g |
|
||||
exists(Parameter p | mc.getTarget().getParameter(i) = p | p.isOut() or p.isRef()) and
|
||||
mc.getArgument(i) = g.getAnAccess() and
|
||||
g.getSourceDeclaration() = f
|
||||
and g.getInitializer() = init
|
||||
and not init instanceof NullLiteral
|
||||
)
|
||||
and not exists(AssignOperation ua, Field g |
|
||||
ua.getLValue().(FieldAccess).getTarget() = g
|
||||
and g.getSourceDeclaration() = f
|
||||
)
|
||||
and not exists(MutatorOperation op |
|
||||
) and
|
||||
not isFieldExternallyInitialized(f) and
|
||||
not exists(f.getAnAttribute()) and
|
||||
not exists(Expr init, Field g |
|
||||
g.getSourceDeclaration() = f and
|
||||
g.getInitializer() = init and
|
||||
not init instanceof NullLiteral
|
||||
) and
|
||||
not exists(AssignOperation ua, Field g |
|
||||
ua.getLValue().(FieldAccess).getTarget() = g and
|
||||
g.getSourceDeclaration() = f
|
||||
) and
|
||||
not exists(MutatorOperation op |
|
||||
op.getAnOperand().(FieldAccess).getTarget().getSourceDeclaration() = f
|
||||
) and
|
||||
exists(Field g |
|
||||
fa.getTarget() = g and
|
||||
g.getSourceDeclaration() = f
|
||||
)
|
||||
and exists(Field g |
|
||||
fa.getTarget() = g
|
||||
and g.getSourceDeclaration() = f
|
||||
)
|
||||
select f, "The field '" + f.getName() + "' is never explicitly assigned a value, yet it is read $@.",
|
||||
fa, "here"
|
||||
select f,
|
||||
"The field '" + f.getName() + "' is never explicitly assigned a value, yet it is read $@.", fa,
|
||||
"here"
|
||||
|
||||
@@ -9,13 +9,14 @@
|
||||
* useless-code
|
||||
* external/cwe/cwe-561
|
||||
*/
|
||||
|
||||
import csharp
|
||||
import DeadCode
|
||||
|
||||
from Field f
|
||||
where
|
||||
not extractionIsStandalone()
|
||||
and f.fromSource()
|
||||
and isDeadField(f)
|
||||
and not f.getDeclaringType().isPartial()
|
||||
not extractionIsStandalone() and
|
||||
f.fromSource() and
|
||||
isDeadField(f) and
|
||||
not f.getDeclaringType().isPartial()
|
||||
select f, "Unused field (or field used from dead method only)"
|
||||
|
||||
@@ -10,13 +10,14 @@
|
||||
* useless-code
|
||||
* external/cwe/cwe-561
|
||||
*/
|
||||
|
||||
import csharp
|
||||
import DeadCode
|
||||
|
||||
from Method m
|
||||
where
|
||||
not extractionIsStandalone()
|
||||
and m.fromSource()
|
||||
and isDeadMethod(m)
|
||||
and not m.getDeclaringType().isPartial()
|
||||
not extractionIsStandalone() and
|
||||
m.fromSource() and
|
||||
isDeadMethod(m) and
|
||||
not m.getDeclaringType().isPartial()
|
||||
select m, "Unused method (or method called from dead method only)"
|
||||
|
||||
Reference in New Issue
Block a user