Add the ql/primary-ql-class-consistency query

This commit is contained in:
Erik Krogh Kristensen
2021-05-27 22:08:18 +00:00
committed by GitHub
parent e01fe66519
commit e8cc0ee453
2 changed files with 34 additions and 5 deletions

View File

@@ -630,7 +630,11 @@ class String extends Literal {
override string getAPrimaryQlClass() { result = "String" }
/** Gets the string value of this literal. */
string getValue() { result = lit.getChild().(Generated::String).getValue() }
string getValue() {
exists(string raw | raw = lit.getChild().(Generated::String).getValue() |
result = raw.substring(1, raw.length() - 1)
)
}
}
/** An integer literal. */
@@ -1198,10 +1202,8 @@ class ModuleExpr extends TModuleExpr, ModuleRef {
override AstNode getParent() {
result = super.getParent() or
result.(PredicateCall).getQualifier() = this
or
result.(PredicateExpr).getQualifier() = this
or
result.(PredicateCall).getQualifier() = this or
result.(PredicateExpr).getQualifier() = this or
result.(Module).getAlias() = this
}
}

View File

@@ -0,0 +1,27 @@
/**
* @name Inconsistent getAPrimaryQlClass predicate
* @description A getAPrimaryQlClass predicate should result in the name of the class.
* @kind problem
* @problem.severity error
* @id ql/primary-ql-class-consistency
* @tags correctness
* @precision low
*/
import ql
from ClassPredicate pred, String constant
where
exists(string className, string constantName |
pred.getParent().getName() = className and
pred.getName() = "getAPrimaryQlClass" and
constant = pred.getBody().(ComparisonFormula).getRightOperand() and
constant.(String).getValue() = constantName and
// might be "Foo::classname", detect by matching with a regexp
not constantName.regexpMatch(".*\\b" + className + "$") and
// ignore constants with "?" in them
not constantName.regexpMatch(".*\\?.*")
)
select pred,
"The getAPrimaryQlClass predicate $@ instead of the class name \"" + pred.getParent().getName() +
"\".", constant, "results in \"" + constant.getValue() + " \""