mirror of
https://github.com/github/codeql.git
synced 2026-04-28 18:25:24 +02:00
Java: Autoformat most queries.
This commit is contained in:
@@ -22,6 +22,6 @@ where
|
||||
dest = cse.getType()
|
||||
) and
|
||||
dest.getGenericType() = src
|
||||
select e, "Casting 'this' to $@, a type parameter of $@, masks an implicit type constraint that should be explicitly stated.",
|
||||
dest, dest.getName(),
|
||||
src, src.getName()
|
||||
select e,
|
||||
"Casting 'this' to $@, a type parameter of $@, masks an implicit type constraint that should be explicitly stated.",
|
||||
dest, dest.getName(), src, src.getName()
|
||||
|
||||
@@ -14,18 +14,11 @@ import java
|
||||
int instanceofCountForIfChain(IfStmt is) {
|
||||
exists(int rest |
|
||||
(
|
||||
if is.getElse() instanceof IfStmt then
|
||||
rest = instanceofCountForIfChain(is.getElse())
|
||||
else
|
||||
rest = 0
|
||||
)
|
||||
and
|
||||
(
|
||||
if is.getCondition() instanceof InstanceOfExpr then
|
||||
result = 1 + rest
|
||||
else
|
||||
result = rest
|
||||
)
|
||||
if is.getElse() instanceof IfStmt
|
||||
then rest = instanceofCountForIfChain(is.getElse())
|
||||
else rest = 0
|
||||
) and
|
||||
(if is.getCondition() instanceof InstanceOfExpr then result = 1 + rest else result = rest)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -36,4 +29,4 @@ where
|
||||
not exists(IfStmt other | is = other.getElse())
|
||||
select is,
|
||||
"This if block performs a chain of " + n +
|
||||
" type tests - consider alternatives, e.g. polymorphism or the visitor pattern."
|
||||
" type tests - consider alternatives, e.g. polymorphism or the visitor pattern."
|
||||
|
||||
@@ -25,5 +25,4 @@ where
|
||||
src != dest and
|
||||
not dest instanceof TypeVariable
|
||||
select e, "Downcasting 'this' from $@ to $@ introduces a dependency cycle between the two types.",
|
||||
src, src.getName(),
|
||||
dest, dest.getName()
|
||||
src, src.getName(), dest, dest.getName()
|
||||
|
||||
@@ -19,6 +19,6 @@ where
|
||||
t = ioe.getExpr().getType() and
|
||||
ct = ioe.getTypeName().getType() and
|
||||
ct.getASupertype*() = t
|
||||
select ioe, "Testing whether 'this' is an instance of $@ in $@ introduces a dependency cycle between the two types.",
|
||||
ct, ct.getName(),
|
||||
t, t.getName()
|
||||
select ioe,
|
||||
"Testing whether 'this' is an instance of $@ in $@ introduces a dependency cycle between the two types.",
|
||||
ct, ct.getName(), t, t.getName()
|
||||
|
||||
@@ -13,7 +13,8 @@ import java
|
||||
|
||||
from EmptyStmt empty, string action
|
||||
where
|
||||
if exists(LoopStmt l | l.getBody() = empty) then (
|
||||
if exists(LoopStmt l | l.getBody() = empty)
|
||||
then (
|
||||
action = "turned into '{}'"
|
||||
) else (
|
||||
action = "deleted"
|
||||
|
||||
@@ -13,9 +13,9 @@
|
||||
import java
|
||||
|
||||
Element elementNamedEnum() {
|
||||
result.(CompilationUnit).getPackage().getName().regexpMatch("(.*\\.|)enum(\\..*|)")
|
||||
or
|
||||
result.(CompilationUnit).getPackage().getName().regexpMatch("(.*\\.|)enum(\\..*|)") or
|
||||
result.getName() = "enum"
|
||||
}
|
||||
|
||||
select elementNamedEnum(), "Code using 'enum' as an identifier will not compile with a recent version of Java."
|
||||
select elementNamedEnum(),
|
||||
"Code using 'enum' as an identifier will not compile with a recent version of Java."
|
||||
|
||||
@@ -14,4 +14,5 @@ import java
|
||||
|
||||
from RefType type, AnnotationType annotation
|
||||
where type.getASupertype() = annotation
|
||||
select type, "Should this class be annotated by '" + annotation.getName() + "', not have it as a super-type?"
|
||||
select type,
|
||||
"Should this class be annotated by '" + annotation.getName() + "', not have it as a super-type?"
|
||||
|
||||
@@ -10,14 +10,13 @@
|
||||
* @tags correctness
|
||||
* reliability
|
||||
*/
|
||||
|
||||
import java
|
||||
import IterableClass
|
||||
|
||||
/** An `Iterable` that is also its own `Iterator`. */
|
||||
class IterableIterator extends Iterable {
|
||||
IterableIterator() {
|
||||
simpleIterator() instanceof ThisAccess
|
||||
}
|
||||
IterableIterator() { simpleIterator() instanceof ThisAccess }
|
||||
}
|
||||
|
||||
/** An `IterableIterator` that never returns any elements. */
|
||||
@@ -26,7 +25,14 @@ class EmptyIterableIterator extends IterableIterator {
|
||||
exists(Method m |
|
||||
m.getDeclaringType().getSourceDeclaration() = this and
|
||||
m.getName() = "hasNext" and
|
||||
m.getBody().(SingletonBlock).getStmt().(ReturnStmt).getResult().(BooleanLiteral).getBooleanValue() = false
|
||||
m
|
||||
.getBody()
|
||||
.(SingletonBlock)
|
||||
.getStmt()
|
||||
.(ReturnStmt)
|
||||
.getResult()
|
||||
.(BooleanLiteral)
|
||||
.getBooleanValue() = false
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,7 @@
|
||||
import java
|
||||
|
||||
predicate complicatedBranch(Stmt branch) {
|
||||
exists(ConditionalExpr ce |
|
||||
ce.getParent*() = branch
|
||||
) or
|
||||
exists(ConditionalExpr ce | ce.getParent*() = branch) or
|
||||
count(MethodAccess a | a.getParent*() = branch) > 1
|
||||
}
|
||||
|
||||
@@ -31,9 +29,11 @@ predicate toCompare(Expr left, Expr right) {
|
||||
exists(IfStmt is, AssignExpr at, AssignExpr ae |
|
||||
at.getParent() = is.getThen() and
|
||||
ae.getParent() = is.getElse()
|
||||
|
|
||||
left = at.getDest() and right = ae.getDest() or
|
||||
left = at.getDest().(VarAccess).getQualifier() and right = ae.getDest().(VarAccess).getQualifier()
|
||||
|
|
||||
left = at.getDest() and right = ae.getDest()
|
||||
or
|
||||
left = at.getDest().(VarAccess).getQualifier() and
|
||||
right = ae.getDest().(VarAccess).getQualifier()
|
||||
)
|
||||
}
|
||||
|
||||
@@ -45,7 +45,8 @@ predicate sameVariable(VarAccess left, VarAccess right) {
|
||||
left.getQualifier() = q1 and
|
||||
sameVariable(q1, q2) and
|
||||
right.getQualifier() = q2
|
||||
) or
|
||||
)
|
||||
or
|
||||
left.isLocal() and right.isLocal()
|
||||
)
|
||||
}
|
||||
@@ -71,4 +72,5 @@ where
|
||||
complicatedBranch(is.getThen()) or
|
||||
complicatedBranch(is.getElse())
|
||||
)
|
||||
select is, "Both branches of this 'if' statement " + what + " - consider using '?' to express intent better."
|
||||
select is,
|
||||
"Both branches of this 'if' statement " + what + " - consider using '?' to express intent better."
|
||||
|
||||
@@ -20,5 +20,4 @@ where
|
||||
not superMethod.isProtected() and
|
||||
not superMethod.isPrivate()
|
||||
select method, "This method does not override $@ because it is private to another package.",
|
||||
superMethod,
|
||||
superMethod.getDeclaringType().getName() + "." + superMethod.getName()
|
||||
superMethod, superMethod.getDeclaringType().getName() + "." + superMethod.getName()
|
||||
|
||||
@@ -18,4 +18,5 @@ from TypeVariable v, RefType bound
|
||||
where
|
||||
v.getATypeBound().getType() = bound and
|
||||
bound.isFinal()
|
||||
select v, "Type '" + bound + "' is final, so <" + v.getName() + " extends " + bound + "> is confusing."
|
||||
select v,
|
||||
"Type '" + bound + "' is final, so <" + v.getName() + " extends " + bound + "> is confusing."
|
||||
|
||||
@@ -14,10 +14,8 @@
|
||||
import java
|
||||
|
||||
RefType anOuterType(TypeVariable var) {
|
||||
var.getGenericCallable().getDeclaringType() = result
|
||||
or
|
||||
var.getGenericType() = result
|
||||
or
|
||||
var.getGenericCallable().getDeclaringType() = result or
|
||||
var.getGenericType() = result or
|
||||
result = anOuterType(var).(NestedType).getEnclosingType()
|
||||
}
|
||||
|
||||
|
||||
@@ -19,10 +19,12 @@ from Expr guard, Expr e, Expr reason, string msg
|
||||
where
|
||||
guard = basicNullGuard(e, _, true) and
|
||||
e = clearlyNotNullExpr(reason) and
|
||||
(if reason instanceof Guard then
|
||||
msg = "This check is useless, $@ cannot be null here, since it is guarded by $@."
|
||||
else if reason != e then
|
||||
msg = "This check is useless, $@ cannot be null here, since $@ always is non-null."
|
||||
else
|
||||
msg = "This check is useless, since $@ always is non-null.")
|
||||
(
|
||||
if reason instanceof Guard
|
||||
then msg = "This check is useless, $@ cannot be null here, since it is guarded by $@."
|
||||
else
|
||||
if reason != e
|
||||
then msg = "This check is useless, $@ cannot be null here, since $@ always is non-null."
|
||||
else msg = "This check is useless, since $@ always is non-null."
|
||||
)
|
||||
select guard, msg, e, e.toString(), reason, reason.toString()
|
||||
|
||||
@@ -17,6 +17,6 @@ where
|
||||
t = ioe.getExpr().getType() and
|
||||
ct = ioe.getTypeName().getType() and
|
||||
ct = t.getASupertype+()
|
||||
select ioe, "There is no need to test whether an instance of $@ is also an instance of $@ - it always is.",
|
||||
t, t.getName(),
|
||||
ct, ct.getName()
|
||||
select ioe,
|
||||
"There is no need to test whether an instance of $@ is also an instance of $@ - it always is.", t,
|
||||
t.getName(), ct, ct.getName()
|
||||
|
||||
@@ -19,13 +19,13 @@ predicate usefulUpcast(CastExpr e) {
|
||||
target = c.getCallee() and
|
||||
// An upcast to the type of the corresponding parameter.
|
||||
e.getType() = target.getParameterType(i)
|
||||
|
|
||||
|
|
||||
// There is an overloaded method/constructor in the class that we might be trying to avoid.
|
||||
exists(Callable other |
|
||||
other.getName() = target.getName() and
|
||||
other.getSourceDeclaration() != target.getSourceDeclaration()
|
||||
|
|
||||
c.(MethodAccess).getReceiverType().(RefType).inherits((Method)other) or
|
||||
|
|
||||
c.(MethodAccess).getReceiverType().(RefType).inherits(other.(Method)) or
|
||||
other = target.(Constructor).getDeclaringType().getAConstructor()
|
||||
)
|
||||
)
|
||||
@@ -36,7 +36,9 @@ predicate usefulUpcast(CastExpr e) {
|
||||
)
|
||||
or
|
||||
// Upcasts that are performed on an operand of a ternary expression.
|
||||
exists(ConditionalExpr ce | e = ce.getTrueExpr().getProperExpr() or e = ce.getFalseExpr().getProperExpr())
|
||||
exists(ConditionalExpr ce |
|
||||
e = ce.getTrueExpr().getProperExpr() or e = ce.getFalseExpr().getProperExpr()
|
||||
)
|
||||
or
|
||||
// Upcasts to raw types.
|
||||
e.getType() instanceof RawType
|
||||
@@ -46,10 +48,13 @@ predicate usefulUpcast(CastExpr e) {
|
||||
// Upcasts that are performed to affect field, private method, or static method resolution.
|
||||
exists(FieldAccess fa | e = fa.getQualifier().getProperExpr() |
|
||||
not e.getExpr().getType().(RefType).inherits(fa.getField())
|
||||
) or
|
||||
)
|
||||
or
|
||||
exists(MethodAccess ma, Method m |
|
||||
e = ma.getQualifier().getProperExpr() and m = ma.getMethod() and (m.isStatic() or m.isPrivate())
|
||||
|
|
||||
e = ma.getQualifier().getProperExpr() and
|
||||
m = ma.getMethod() and
|
||||
(m.isStatic() or m.isPrivate())
|
||||
|
|
||||
not e.getExpr().getType().(RefType).inherits(m)
|
||||
)
|
||||
}
|
||||
@@ -63,6 +68,5 @@ where
|
||||
) and
|
||||
dest = src.getASupertype+() and
|
||||
not usefulUpcast(e)
|
||||
select e, "There is no need to upcast from $@ to $@ - the conversion can be done implicitly.",
|
||||
src, src.getName(),
|
||||
dest, dest.getName()
|
||||
select e, "There is no need to upcast from $@ to $@ - the conversion can be done implicitly.", src,
|
||||
src.getName(), dest, dest.getName()
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
* @tags correctness
|
||||
* reliability
|
||||
*/
|
||||
|
||||
import java
|
||||
import IterableClass
|
||||
|
||||
@@ -18,18 +19,25 @@ predicate iteratorWrapper(Iterable it, Field f, boolean wrap) {
|
||||
// declares a final or effectively final field ...
|
||||
f.getDeclaringType().getSourceDeclaration() = it and
|
||||
(
|
||||
f.isFinal() or
|
||||
f.isFinal()
|
||||
or
|
||||
(
|
||||
strictcount(f.getAnAssignedValue()) = 1 and
|
||||
f.getAnAssignedValue().getEnclosingCallable() instanceof InitializerMethod
|
||||
)
|
||||
) and
|
||||
// ... whose type is a sub-type of `java.util.Iterator` and ...
|
||||
f.getType().(RefType).getASupertype*().getSourceDeclaration().hasQualifiedName("java.util", "Iterator") and
|
||||
f
|
||||
.getType()
|
||||
.(RefType)
|
||||
.getASupertype*()
|
||||
.getSourceDeclaration()
|
||||
.hasQualifiedName("java.util", "Iterator") and
|
||||
// ... whose value is returned by the `iterator()` method of this class ...
|
||||
exists(Expr iterator | iterator = it.simpleIterator() |
|
||||
// ... either directly ...
|
||||
iterator = f.getAnAccess() and wrap = false or
|
||||
iterator = f.getAnAccess() and wrap = false
|
||||
or
|
||||
// ... or wrapped in another Iterator.
|
||||
exists(ClassInstanceExpr cie | cie = iterator and wrap = true |
|
||||
cie.getAnArgument() = f.getAnAccess() or
|
||||
@@ -41,9 +49,11 @@ predicate iteratorWrapper(Iterable it, Field f, boolean wrap) {
|
||||
from Iterable i, Field f, boolean wrap, string appearto, string iteratorbasedon
|
||||
where
|
||||
iteratorWrapper(i, f, wrap) and
|
||||
( wrap = true and appearto = "appear to " and iteratorbasedon = "an iterator based on " or
|
||||
(
|
||||
wrap = true and appearto = "appear to " and iteratorbasedon = "an iterator based on "
|
||||
or
|
||||
wrap = false and appearto = "" and iteratorbasedon = ""
|
||||
)
|
||||
select i, "This class implements Iterable, but does not " + appearto + "support multiple iterations," +
|
||||
" since its iterator method always returns " + iteratorbasedon + "the same $@.",
|
||||
f, "iterator"
|
||||
select i,
|
||||
"This class implements Iterable, but does not " + appearto + "support multiple iterations," +
|
||||
" since its iterator method always returns " + iteratorbasedon + "the same $@.", f, "iterator"
|
||||
|
||||
Reference in New Issue
Block a user