Merge pull request #6994 from erik-krogh/redundant-cast

Approved by RasmusWL, aschackmull, esbena, geoffw0, hvitved, nickrolfe
This commit is contained in:
CodeQL CI
2021-11-02 03:45:48 -07:00
committed by GitHub
78 changed files with 131 additions and 163 deletions

View File

@@ -150,7 +150,7 @@ private module ControlFlowGraphImpl {
* `TypeThrowable` which results in both `TypeError` and `TypeRuntimeException`.
*/
UncheckedThrowableType getAnUncheckedSubtype() {
result = this.(UncheckedThrowableType)
result = this
or
result instanceof TypeError and this instanceof TypeThrowable
or

View File

@@ -665,7 +665,7 @@ final class GenericTypeNode extends PrintAstNode, TGenericTypeNode {
override Location getLocation() { none() }
override ElementNode getChild(int childIndex) {
result.getElement().(TypeVariable) = ty.getTypeParameter(childIndex)
result.getElement() = ty.getTypeParameter(childIndex)
}
/**
@@ -686,7 +686,7 @@ final class GenericCallableNode extends PrintAstNode, TGenericCallableNode {
override string toString() { result = "(Generic Parameters)" }
override ElementNode getChild(int childIndex) {
result.getElement().(TypeVariable) = c.getTypeParameter(childIndex)
result.getElement() = c.getTypeParameter(childIndex)
}
/**

View File

@@ -150,7 +150,7 @@ private Type parameterForSubTypes(ParameterizedType type) {
lowerBound = arg.(Wildcard).getLowerBoundType()
|
// `T super Foo` implies that `Foo`, or any super-type of `Foo`, may be represented.
lowerBound.(RefType).getAnAncestor() = result
lowerBound.getAnAncestor() = result
)
)
}

View File

@@ -567,7 +567,7 @@ class ThrowStmt extends Stmt, @throwstmt {
or
exists(Stmt mid |
mid = this.findEnclosing() and
not exists(this.catchClauseForThis(mid.(TryStmt))) and
not exists(this.catchClauseForThis(mid)) and
result = mid.getEnclosingStmt()
)
}
@@ -575,7 +575,7 @@ class ThrowStmt extends Stmt, @throwstmt {
private CatchClause catchClauseForThis(TryStmt try) {
result = try.getACatchClause() and
result.getEnclosingCallable() = this.getEnclosingCallable() and
this.getExpr().getType().(RefType).hasSupertype*(result.getVariable().getType().(RefType)) and
this.getExpr().getType().(RefType).hasSupertype*(result.getVariable().getType()) and
not this.getEnclosingStmt+() = result
}

View File

@@ -511,7 +511,7 @@ class RefType extends Type, Annotatable, Modifiable, @reftype {
this.getSourceDeclaration().inherits(f)
)
or
this.hasMethod(m.(Method), _)
this.hasMethod(m, _)
}
/** Holds if this is a top-level type, which is not nested inside any other types. */

View File

@@ -18,12 +18,12 @@ predicate isLive(Callable c) {
* would imply the liveness of `c`.
*/
Callable possibleLivenessCause(Callable c, string reason) {
c.(Method).overridesOrInstantiates(result.(Method)) and
c.(Method).overridesOrInstantiates(result) and
reason = "is overridden or instantiated by"
or
result.calls(c) and reason = "calls"
or
result.callsConstructor(c.(Constructor)) and reason = "calls constructor"
result.callsConstructor(c) and reason = "calls constructor"
or
exists(ClassInstanceExpr e | e.getEnclosingCallable() = result |
e.getConstructor() = c and reason = "constructs"
@@ -93,8 +93,8 @@ class SuppressedConstructor extends Constructor {
not this.isDefaultConstructor() and
// Verify that there is only one statement, which is the `super()` call. This exists
// even for empty constructors.
this.getBody().(BlockStmt).getNumStmt() = 1 and
this.getBody().(BlockStmt).getAStmt().(SuperConstructorInvocationStmt).getNumArgument() = 0 and
this.getBody().getNumStmt() = 1 and
this.getBody().getAStmt().(SuperConstructorInvocationStmt).getNumArgument() = 0 and
// A constructor that is called is not acting to suppress the default constructor. We permit
// calls from suppressed and default constructors - in both cases, they can only come from
// sub-class constructors.
@@ -243,7 +243,7 @@ class DeadMethod extends Callable {
) and
not (
this.(Method).isAbstract() and
exists(Method m | m.overridesOrInstantiates+(this.(Method)) | isLive(m))
exists(Method m | m.overridesOrInstantiates+(this) | isLive(m))
) and
// A getter or setter associated with a live JPA field.
//

View File

@@ -262,7 +262,7 @@ class ManagedBeanImplEntryPoint extends EntryPoint, RegisteredManagedBeanImpl {
// Find the method that will be called for each method on each managed bean that this class
// implements.
this.inherits(result) and
result.(Method).overrides(this.getAnImplementedManagedBean().getAMethod())
result.overrides(this.getAnImplementedManagedBean().getAMethod())
}
}

View File

@@ -54,7 +54,7 @@ class JaxbType extends Class {
this.getAnAnnotation() = a and
a.getType().(JaxbAnnotationType).hasName("XmlAccessorType")
|
result.getAnAccess() = a.getValue("value").(VarAccess)
result.getAnAccess() = a.getValue("value")
)
}

View File

@@ -668,7 +668,7 @@ Type inheritsMatchingMethodExceptThrows(SessionEJB ejb, Method m) {
sig = n.getSignature() and
sig = m.getSignature() and
exists(Exception ex | ex = n.getAnException() and not throwsExplicitUncheckedException(n, ex) |
not ex.getType().(RefType).hasSupertype*(m.getAnException().getType()) and
not ex.getType().hasSupertype*(m.getAnException().getType()) and
result = ex.getType()
)
)
@@ -717,7 +717,7 @@ Type inheritsMatchingCreateMethodExceptThrows(StatefulSessionEJB ejb, EjbInterfa
exists(Exception ex |
ex = cm.getAnException() and not throwsExplicitUncheckedException(cm, ex)
|
not ex.getType().(RefType).hasSupertype*(icm.getAnException().getType()) and
not ex.getType().hasSupertype*(icm.getAnException().getType()) and
result = ex.getType()
)
)
@@ -732,7 +732,7 @@ Type inheritsMatchingCreateMethodExceptThrows(StatefulSessionEJB ejb, EjbInterfa
exists(Exception ex |
ex = im.getAnException() and not throwsExplicitUncheckedException(im, ex)
|
not ex.getType().(RefType).hasSupertype*(icm.getAnException().getType()) and
not ex.getType().hasSupertype*(icm.getAnException().getType()) and
result = ex.getType()
)
)

View File

@@ -141,8 +141,7 @@ class ExternalAPIUsedWithUntrustedData extends TExternalAPI {
|
this = TExternalAPIParameter(m, index) and
result =
m.getDeclaringType().(RefType).getQualifiedName() + "." + m.getSignature() + " [" +
indexString + "]"
m.getDeclaringType().getQualifiedName() + "." + m.getSignature() + " [" + indexString + "]"
)
}
}

View File

@@ -395,9 +395,7 @@ class MavenRepo extends Folder {
/**
* Gets a Jar file contained within this repository.
*/
File getAJarFile() {
result = this.getAChildContainer*().(File) and result.getExtension() = "jar"
}
File getAJarFile() { result = this.getAChildContainer*() and result.getExtension() = "jar" }
/**
* Gets any jar artifacts in this repository that match the POM project definition. This is an

View File

@@ -25,7 +25,7 @@ predicate usefulUpcast(CastExpr e) {
other.getName() = target.getName() and
other.getSourceDeclaration() != target.getSourceDeclaration()
|
c.(MethodAccess).getReceiverType().(RefType).inherits(other.(Method)) or
c.(MethodAccess).getReceiverType().inherits(other.(Method)) or
other = target.(Constructor).getDeclaringType().getAConstructor()
)
)

View File

@@ -25,7 +25,7 @@ class DangerousAssignOpExpr extends AssignOp {
}
}
predicate problematicCasting(Type t, Expr e) { e.getType().(NumType).widerThan(t.(NumType)) }
predicate problematicCasting(Type t, Expr e) { e.getType().(NumType).widerThan(t) }
from DangerousAssignOpExpr a, Expr e
where

View File

@@ -45,7 +45,7 @@ private predicate closeableType(RefType t) {
class SqlResourceOpeningMethodAccess extends MethodAccess {
SqlResourceOpeningMethodAccess() {
exists(Method m | this.getMethod() = m |
m.getDeclaringType().(RefType).hasQualifiedName("java.sql", _) and
m.getDeclaringType().hasQualifiedName("java.sql", _) and
m.getReturnType().(RefType).hasQualifiedName("java.sql", _) and
m.getName().regexpMatch("(create|prepare|execute).*") and
closeableType(m.getReturnType()) and

View File

@@ -14,9 +14,7 @@ private import semmle.code.java.controlflow.internal.GuardsLogic
predicate narrowerThanOrEqualTo(ArithExpr exp, NumType numType) {
exp.getType().(NumType).widerThan(numType)
implies
exists(CastExpr cast | cast.getAChildExpr() = exp |
numType.widerThanOrEqualTo(cast.getType().(NumType))
)
exists(CastExpr cast | cast.getAChildExpr() = exp | numType.widerThanOrEqualTo(cast.getType()))
}
private Guard sizeGuard(SsaVariable v, boolean branch, boolean upper) {

View File

@@ -9,7 +9,7 @@ class NumericNarrowingCastExpr extends CastExpr {
exists(NumericType sourceType, NumericType targetType |
sourceType = getExpr().getType() and targetType = getType()
|
not targetType.(NumType).widerThanOrEqualTo(sourceType.(NumType))
not targetType.(NumType).widerThanOrEqualTo(sourceType)
)
}
}

View File

@@ -159,7 +159,7 @@ predicate badMethodAccessLockOrder(
MethodAccess outerAccess, MethodAccess innerAccess, MethodAccess other
) {
exists(Synched outer, Synched inner |
inner.(MethodAccess) = innerAccess and
inner = innerAccess and
inner = outer.getInnerSynch() and
inner.getLockType() = outer.getLockType() and
exists(Parameter p, int i | outer.(Method).getAParameter() = p and p.getPosition() = i |