mirror of
https://github.com/github/codeql.git
synced 2026-06-10 07:21:12 +02:00
46 lines
1.5 KiB
Plaintext
46 lines
1.5 KiB
Plaintext
/**
|
|
* @name Potentially incorrect CompareTo(...) signature
|
|
* @description The declaring type of a method with signature `CompareTo(T)` does not implement `IComparable<T>`.
|
|
* @kind problem
|
|
* @problem.severity warning
|
|
* @precision medium
|
|
* @id cs/wrong-compareto-signature
|
|
* @tags reliability
|
|
* maintainability
|
|
*/
|
|
|
|
import csharp
|
|
import semmle.code.csharp.frameworks.System
|
|
|
|
predicate implementsIComparable(ValueOrRefType t, Type paramType) {
|
|
exists(ConstructedType ct | t.getABaseType+() = ct |
|
|
ct = any(SystemIComparableTInterface i).getAConstructedGeneric() and
|
|
paramType = ct.getATypeArgument()
|
|
)
|
|
or
|
|
t instanceof SystemIComparableTInterface
|
|
or
|
|
t.getABaseType*() instanceof SystemIComparableInterface and
|
|
paramType instanceof ObjectType
|
|
}
|
|
|
|
predicate compareToMethod(Method m, Type paramType) {
|
|
m.hasName("CompareTo") and
|
|
m.fromSource() and
|
|
m.isPublic() and
|
|
m.getReturnType() instanceof IntType and
|
|
m.getNumberOfParameters() = 1 and
|
|
paramType = m.getAParameter().getType()
|
|
}
|
|
|
|
from Method m, RefType declaringType, Type actualParamType
|
|
where
|
|
m.isSourceDeclaration() and
|
|
declaringType = m.getDeclaringType() and
|
|
compareToMethod(m, actualParamType) and
|
|
not implementsIComparable(declaringType, actualParamType)
|
|
select m,
|
|
"The parameter of this 'CompareTo' method is of type $@, but $@ does not implement 'IComparable<$@>'.",
|
|
actualParamType, actualParamType.getName(), declaringType, declaringType.getName(),
|
|
actualParamType, actualParamType.getName()
|