mirror of
https://github.com/github/codeql.git
synced 2025-12-17 01:03:14 +01:00
Move 'snippet' queries to 'snippets' folders
This commit is contained in:
16
java/ql/examples/snippets/arrayaccess.ql
Normal file
16
java/ql/examples/snippets/arrayaccess.ql
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @name Array access
|
||||
* @description Finds array access expressions with an index expression
|
||||
* consisting of a unary assignment
|
||||
* @tags array
|
||||
* access
|
||||
* index
|
||||
* unary
|
||||
* assignment
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from ArrayAccess a
|
||||
where a.getIndexExpr() instanceof UnaryAssignExpr
|
||||
select a
|
||||
15
java/ql/examples/snippets/castexpr.ql
Normal file
15
java/ql/examples/snippets/castexpr.ql
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name Cast expressions
|
||||
* @description Finds casts from a floating point type to an integer type
|
||||
* @tags cast
|
||||
* integer
|
||||
* float
|
||||
* type
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from CastExpr c
|
||||
where c.getExpr().getType() instanceof FloatingPointType
|
||||
and c.getType() instanceof IntegralType
|
||||
select c
|
||||
13
java/ql/examples/snippets/catch_exception.ql
Normal file
13
java/ql/examples/snippets/catch_exception.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Catch exception
|
||||
* @description Finds places where we catch exceptions of type com.example.AnException
|
||||
* @tags catch
|
||||
* try
|
||||
* exception
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from CatchClause catch
|
||||
where catch.getACaughtType().hasQualifiedName("com.example", "AnException")
|
||||
select catch
|
||||
13
java/ql/examples/snippets/constructor_call.ql
Normal file
13
java/ql/examples/snippets/constructor_call.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Call to constructor
|
||||
* @description Finds places where we call `new com.example.Class(...)`
|
||||
* @tags call
|
||||
* constructor
|
||||
* new
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from ClassInstanceExpr new
|
||||
where new.getConstructedType().hasQualifiedName("com.example", "Class")
|
||||
select new
|
||||
13
java/ql/examples/snippets/emptyblock.ql
Normal file
13
java/ql/examples/snippets/emptyblock.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Empty blocks
|
||||
* @description Finds empty block statements
|
||||
* @tags empty
|
||||
* block
|
||||
* statement
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Block blk
|
||||
where blk.getNumStmt() = 0
|
||||
select blk
|
||||
16
java/ql/examples/snippets/emptythen.ql
Normal file
16
java/ql/examples/snippets/emptythen.ql
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @name If statements with empty then branch
|
||||
* @description Finds 'if' statements where the 'then' branch is
|
||||
* an empty block statement
|
||||
* @tags if
|
||||
* then
|
||||
* empty
|
||||
* conditional
|
||||
* branch
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from IfStmt i
|
||||
where i.getThen().(Block).getNumStmt() = 0
|
||||
select i
|
||||
13
java/ql/examples/snippets/eq_true.ql
Normal file
13
java/ql/examples/snippets/eq_true.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Equality test on boolean
|
||||
* @description Finds tests like `==true`, `==false`, `!=true`, `!=false`
|
||||
* @tags equals
|
||||
* test
|
||||
* boolean
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from EqualityTest eq
|
||||
where eq.getAnOperand() instanceof BooleanLiteral
|
||||
select eq
|
||||
16
java/ql/examples/snippets/extend_class.ql
Normal file
16
java/ql/examples/snippets/extend_class.ql
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @name Class extends/implements
|
||||
* @description Finds classes/interfaces that extend/implement com.example.Class
|
||||
* @tags class
|
||||
* extends
|
||||
* implements
|
||||
* overrides
|
||||
* subtype
|
||||
* supertype
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from RefType type
|
||||
where type.getASupertype+().hasQualifiedName("com.example", "Class")
|
||||
select type
|
||||
14
java/ql/examples/snippets/field_read.ql
Normal file
14
java/ql/examples/snippets/field_read.ql
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @name Read of field
|
||||
* @description Finds reads of aField (defined on com.example.Class)
|
||||
* @tags field
|
||||
* read
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Field f, FieldRead read
|
||||
where f.hasName("aField")
|
||||
and f.getDeclaringType().hasQualifiedName("com.example", "Class")
|
||||
and f = read.getField()
|
||||
select read
|
||||
12
java/ql/examples/snippets/integer_literal.ql
Normal file
12
java/ql/examples/snippets/integer_literal.ql
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @name Integer literal
|
||||
* @description Finds places where we use the integer literal `0`
|
||||
* @tags integer
|
||||
* literal
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from IntegerLiteral literal
|
||||
where literal.getLiteral().toInt() = 0
|
||||
select literal
|
||||
14
java/ql/examples/snippets/method_call.ql
Normal file
14
java/ql/examples/snippets/method_call.ql
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @name Call to method
|
||||
* @description Finds calls to com.example.Class.methodName
|
||||
* @tags call
|
||||
* method
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from MethodAccess call, Method method
|
||||
where call.getMethod() = method
|
||||
and method.hasName("methodName")
|
||||
and method.getDeclaringType().hasQualifiedName("com.example", "Class")
|
||||
select call
|
||||
14
java/ql/examples/snippets/mutualrecursion.ql
Normal file
14
java/ql/examples/snippets/mutualrecursion.ql
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @name Mutual recursion
|
||||
* @description Finds pairs of methods that call each other
|
||||
* @tags method
|
||||
* recursion
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Method m, Method n
|
||||
where exists(MethodAccess ma | ma.getCaller() = m and ma.getCallee() = n)
|
||||
and exists(MethodAccess ma | ma.getCaller() = n and ma.getCallee() = m)
|
||||
and m != n
|
||||
select m, n
|
||||
13
java/ql/examples/snippets/nativemethod.ql
Normal file
13
java/ql/examples/snippets/nativemethod.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Native methods
|
||||
* @description Finds methods that are native
|
||||
* @tags method
|
||||
* native
|
||||
* modifier
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Method m
|
||||
where m.isNative()
|
||||
select m
|
||||
18
java/ql/examples/snippets/null_argument.ql
Normal file
18
java/ql/examples/snippets/null_argument.ql
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @name Add null to collection
|
||||
* @description Finds places where we add null to a collection
|
||||
* @tags null
|
||||
* parameter
|
||||
* argument
|
||||
* collection
|
||||
* add
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from MethodAccess call, Method add
|
||||
where call.getMethod().overrides*(add)
|
||||
and add.hasName("add")
|
||||
and add.getDeclaringType().getSourceDeclaration().hasQualifiedName("java.util", "Collection")
|
||||
and call.getAnArgument() instanceof NullLiteral
|
||||
select call
|
||||
14
java/ql/examples/snippets/override_method.ql
Normal file
14
java/ql/examples/snippets/override_method.ql
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @name Override of method
|
||||
* @description Finds methods that override com.example.Class.baseMethod
|
||||
* @tags method
|
||||
* override
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Method override, Method base
|
||||
where base.hasName("baseMethod")
|
||||
and base.getDeclaringType().hasQualifiedName("com.example", "Class")
|
||||
and override.overrides+(base)
|
||||
select override
|
||||
13
java/ql/examples/snippets/qualifiedthis.ql
Normal file
13
java/ql/examples/snippets/qualifiedthis.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Qualified 'this' access
|
||||
* @description Finds 'this' accesses that are qualified by a type name
|
||||
* @tags this
|
||||
* access
|
||||
* qualifier
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from ThisAccess t
|
||||
where exists(t.getQualifier())
|
||||
select t
|
||||
13
java/ql/examples/snippets/returnstatement.ql
Normal file
13
java/ql/examples/snippets/returnstatement.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Return statements
|
||||
* @description Finds return statements that return 'null'
|
||||
* @tags return
|
||||
* statement
|
||||
* null
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from ReturnStmt r
|
||||
where r.getResult() instanceof NullLiteral
|
||||
select r
|
||||
12
java/ql/examples/snippets/singletonblock.ql
Normal file
12
java/ql/examples/snippets/singletonblock.ql
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @name Singleton blocks
|
||||
* @description Finds block statements containing a single statement
|
||||
* @tags block
|
||||
* statement
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Block b
|
||||
where b.getNumStmt() = 1
|
||||
select b
|
||||
16
java/ql/examples/snippets/switchcase.ql
Normal file
16
java/ql/examples/snippets/switchcase.ql
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @name Switch statement case missing
|
||||
* @description Finds switch statements with a missing enum constant case and no default case
|
||||
* @tags switch
|
||||
* case
|
||||
* enum
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from SwitchStmt switch, EnumType enum, EnumConstant missing
|
||||
where switch.getExpr().getType() = enum
|
||||
and missing.getDeclaringType() = enum
|
||||
and not switch.getAConstCase().getValue() = missing.getAnAccess()
|
||||
and not exists(switch.getDefaultCase())
|
||||
select switch
|
||||
13
java/ql/examples/snippets/synchronizedmethod.ql
Normal file
13
java/ql/examples/snippets/synchronizedmethod.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Synchronized methods
|
||||
* @description Finds methods that are synchronized
|
||||
* @tags method
|
||||
* synchronized
|
||||
* modifier
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Method m
|
||||
where m.isSynchronized()
|
||||
select m
|
||||
15
java/ql/examples/snippets/ternaryconditional.ql
Normal file
15
java/ql/examples/snippets/ternaryconditional.ql
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name Conditional expressions
|
||||
* @description Finds conditional expressions of the form '... ? ... : ...'
|
||||
* where the types of the resulting expressions differ
|
||||
* @tags conditional
|
||||
* type
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from ConditionalExpr e
|
||||
where e.getTrueExpr().getType() != e.getFalseExpr().getType()
|
||||
and not e.getTrueExpr().getType() instanceof NullType
|
||||
and not e.getFalseExpr().getType() instanceof NullType
|
||||
select e
|
||||
12
java/ql/examples/snippets/throw_exception.ql
Normal file
12
java/ql/examples/snippets/throw_exception.ql
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @name Throw exception of type
|
||||
* @description Finds places where we throw com.example.AnException or one of its subtypes
|
||||
* @tags throw
|
||||
* exception
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from ThrowStmt throw
|
||||
where throw.getThrownExceptionType().getASupertype*().hasQualifiedName("com.example", "AnException")
|
||||
select throw, "Don't throw com.example.AnException"
|
||||
12
java/ql/examples/snippets/todocomment.ql
Normal file
12
java/ql/examples/snippets/todocomment.ql
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @name TODO comments
|
||||
* @description Finds comments containing the word "TODO"
|
||||
* @tags comment
|
||||
* TODO
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from JavadocText c
|
||||
where c.getText().regexpMatch("(?si).*\\bTODO\\b.*")
|
||||
select c
|
||||
13
java/ql/examples/snippets/toomanyparams.ql
Normal file
13
java/ql/examples/snippets/toomanyparams.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Methods with many parameters
|
||||
* @description Finds methods with more than ten parameters
|
||||
* @tags method
|
||||
* parameter
|
||||
* argument
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Method m
|
||||
where m.getNumberOfParameters() > 10
|
||||
select m
|
||||
15
java/ql/examples/snippets/tryfinally.ql
Normal file
15
java/ql/examples/snippets/tryfinally.ql
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name Try-finally statements
|
||||
* @description Finds try-finally statements without a catch clause
|
||||
* @tags try
|
||||
* finally
|
||||
* catch
|
||||
* exceptions
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from TryStmt t
|
||||
where exists(t.getFinally())
|
||||
and not exists(t.getACatchClause())
|
||||
select t
|
||||
13
java/ql/examples/snippets/unusedlocalvar.ql
Normal file
13
java/ql/examples/snippets/unusedlocalvar.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Unused local variable
|
||||
* @description Finds local variables that are not accessed
|
||||
* @tags variable
|
||||
* local
|
||||
* access
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from LocalVariableDecl v
|
||||
where not exists(v.getAnAccess())
|
||||
select v
|
||||
15
java/ql/examples/snippets/unusedmethod.ql
Normal file
15
java/ql/examples/snippets/unusedmethod.ql
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name Unused private method
|
||||
* @description Finds private methods that are not accessed
|
||||
* @tags method
|
||||
* access
|
||||
* private
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Method m
|
||||
where m.isPrivate()
|
||||
and not exists(m.getAReference())
|
||||
and not m instanceof InitializerMethod
|
||||
select m
|
||||
12
java/ql/examples/snippets/unusedparam.ql
Normal file
12
java/ql/examples/snippets/unusedparam.ql
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @name Unused parameter
|
||||
* @description Finds parameters that are not accessed
|
||||
* @tags parameter
|
||||
* access
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Parameter p
|
||||
where not exists(p.getAnAccess())
|
||||
select p
|
||||
15
java/ql/examples/snippets/voidreturntype.ql
Normal file
15
java/ql/examples/snippets/voidreturntype.ql
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* @name Methods without return type
|
||||
* @description Finds methods whose return type is 'void'
|
||||
* @tags method
|
||||
* void
|
||||
* modifier
|
||||
* return
|
||||
* type
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Method m
|
||||
where m.getReturnType() instanceof VoidType
|
||||
select m
|
||||
13
java/ql/examples/snippets/volatilefield.ql
Normal file
13
java/ql/examples/snippets/volatilefield.ql
Normal file
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* @name Fields declared volatile
|
||||
* @description Finds fields with a 'volatile' modifier
|
||||
* @tags field
|
||||
* volatile
|
||||
* synchronization
|
||||
*/
|
||||
|
||||
import java
|
||||
|
||||
from Field f
|
||||
where f.isVolatile()
|
||||
select f
|
||||
Reference in New Issue
Block a user