|
| 1 | +/** |
| 2 | + * @name Names only differing by case |
| 3 | + * @description Names only differing by case can cause confusion. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity warning |
| 6 | + * @id ql/names-only-differing-by-case |
| 7 | + * @tags correctness |
| 8 | + * maintainability |
| 9 | + * @precision high |
| 10 | + */ |
| 11 | + |
| 12 | +import ql |
| 13 | +import codeql_ql.style.AcronymsShouldBeCamelCaseQuery as Acronyms |
| 14 | +private import codeql_ql.style.NodeName as NodeName |
| 15 | + |
| 16 | +string getName(AstNode node, string kind, YAML::QLPack pack) { |
| 17 | + result = NodeName::getName(node, kind) and |
| 18 | + node.getLocation().getFile() = pack.getAFileInPack() and |
| 19 | + not node.hasAnnotation("deprecated") and // deprecated nodes are not interesting |
| 20 | + not node.getLocation().getFile().getBaseName() = "TreeSitter.qll" and // auto-generated, is sometimes bad. |
| 21 | + not node.getLocation().getFile().getExtension() = "ql" // .ql files cannot be imported, so no risk of conflict |
| 22 | +} |
| 23 | + |
| 24 | +// get better join-order by getting all the relevant information in a single utility predicate |
| 25 | +pragma[nomagic] |
| 26 | +string getNameAndPack(AstNode node, string kind, string lower, YAML::QLPack pack) { |
| 27 | + result = getName(node, kind, pack) and lower = result.toLowerCase() |
| 28 | +} |
| 29 | + |
| 30 | +predicate problem(string name1, AstNode node1, string name2, string kind) { |
| 31 | + exists(string lower, YAML::QLPack pack1, YAML::QLPack pack2 | |
| 32 | + name1 = getNameAndPack(node1, kind, lower, pack1) and |
| 33 | + name1.length() >= 2 and |
| 34 | + pack2 = pack1.getADependency*() and |
| 35 | + name2 = getNameAndPack(_, kind, lower, pack2) and // TODO: Get if it's a dependency pack in the alert-message. |
| 36 | + name1 != name2 and |
| 37 | + kind != "variable" // these are benign, and plentyful... |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +string prettyPluralKind(string kind) { |
| 42 | + kind = "class" and result = "classes" |
| 43 | + or |
| 44 | + kind = "classlessPredicate" and result = "predicates" |
| 45 | + or |
| 46 | + kind = "classPredicate" and result = "class predicates" |
| 47 | + or |
| 48 | + kind = "newtypeBranch" and result = "newtype branches" |
| 49 | + or |
| 50 | + kind = "newtype" and result = "newtypes" |
| 51 | + or |
| 52 | + kind = "variable" and result = "variables" |
| 53 | + or |
| 54 | + kind = "field" and result = "fields" |
| 55 | + or |
| 56 | + kind = "module" and result = "modules" |
| 57 | +} |
| 58 | + |
| 59 | +from string name1, AstNode node, string name2, string kind |
| 60 | +where problem(name1, node, name2, kind) and not name1.toLowerCase() = "geturl" |
| 61 | +select node, |
| 62 | + name1 + " is only different by casing from " + name2 + " that is used elsewhere for " + |
| 63 | + prettyPluralKind(kind) + "." |
0 commit comments