diff --git a/ql/src/codeql_ql/ast/Ast.qll b/ql/src/codeql_ql/ast/Ast.qll index 94535bfc5a4..b615ec026eb 100644 --- a/ql/src/codeql_ql/ast/Ast.qll +++ b/ql/src/codeql_ql/ast/Ast.qll @@ -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 } } diff --git a/ql/src/queries/style/GetAPrimaryQlClassConsistency.ql b/ql/src/queries/style/GetAPrimaryQlClassConsistency.ql new file mode 100644 index 00000000000..5ed0069d0c1 --- /dev/null +++ b/ql/src/queries/style/GetAPrimaryQlClassConsistency.ql @@ -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() + " \""