mirror of
https://github.com/github/codeql.git
synced 2026-02-08 03:01:10 +01:00
Add the ql/primary-ql-class-consistency query
This commit is contained in:
committed by
GitHub
parent
e01fe66519
commit
e8cc0ee453
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
27
ql/src/queries/style/GetAPrimaryQlClassConsistency.ql
Normal file
27
ql/src/queries/style/GetAPrimaryQlClassConsistency.ql
Normal 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() + " \""
|
||||
Reference in New Issue
Block a user