From 79485ec5da29a2ad89cd55ed244b49fe547e51d4 Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Wed, 13 Oct 2021 09:01:54 +0000 Subject: [PATCH] New query: Singleton set literal. --- ql/src/codeql_ql/ast/Ast.qll | 2 +- ql/src/queries/style/SingletonSetLiteral.ql | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 ql/src/queries/style/SingletonSetLiteral.ql diff --git a/ql/src/codeql_ql/ast/Ast.qll b/ql/src/codeql_ql/ast/Ast.qll index 4d8da8d8ee5..367355e2e5d 100644 --- a/ql/src/codeql_ql/ast/Ast.qll +++ b/ql/src/codeql_ql/ast/Ast.qll @@ -1926,7 +1926,7 @@ class Set extends TSet, Expr { Set() { this = TSet(set) } /** - * Gets the ith element in the set literal expression. + * Gets the `i`th element in this set literal expression. */ Expr getElement(int i) { toGenerated(result) = set.getChild(i) } diff --git a/ql/src/queries/style/SingletonSetLiteral.ql b/ql/src/queries/style/SingletonSetLiteral.ql new file mode 100644 index 00000000000..c05b3dca71d --- /dev/null +++ b/ql/src/queries/style/SingletonSetLiteral.ql @@ -0,0 +1,15 @@ +/** + * @name Singleton set literal + * @description A singleton set literal can be replaced by its member. + * @kind problem + * @problem.severity warning + * @id ql/singleton-set-literal + * @tags maintainability + * @precision very-high + */ + +import ql + +from Set setlit +where exists(unique(Expr e | e = setlit.getElement(_))) +select setlit, "Singleton set literal can be replaced by its member."