Move 'snippet' queries to 'snippets' folders

This commit is contained in:
Arthur Baars
2019-07-25 15:38:25 +02:00
parent 30860daac4
commit bdce7d07c1
151 changed files with 0 additions and 0 deletions

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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"

View 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

View 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

View 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

View 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

View 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

View 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

View 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

View 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