more precise warning message for implicit string/number conversions

This commit is contained in:
Erik Krogh Kristensen
2020-03-30 10:19:04 +02:00
parent 1baf5df342
commit f55005a0ec
3 changed files with 26 additions and 2 deletions

View File

@@ -162,7 +162,26 @@ abstract class NullOrUndefinedConversion extends ImplicitConversion {
class PlusConversion extends NullOrUndefinedConversion {
PlusConversion() { parent instanceof AddExpr or parent instanceof AssignAddExpr }
override string getConversionTarget() { result = "number or string" }
override string getConversionTarget() {
result = getDefiniteCousinType()
or
not exists(getDefiniteCousinType()) and
result = "number or string"
}
/**
* Gets the cousin of this implicit conversion.
* E.g. if this is `a` in the expression `a + b`, then the cousin is `b`.
*/
private Expr getCousin() { result = parent.getAChild() and not result = this.getEnclosingExpr() }
/**
* Gets the unique type of the cousin expression, if that type is `string` or `number`.
*/
private string getDefiniteCousinType() {
result = unique(InferredType t | t = getCousin().flow().analyze().getAType()).getTypeofTag() and
result = ["string", "number"]
}
}
/**