Error in Base Query right after Data Flow Analysis Integration —— Empty Relation #14995
-
Hello CodeQL Community, Background: Error Message: Basic Query:
Advanced Query(additional variables and logic for data flow analysis on the expected output of the assertion in unit tests, if being a variable).
Issue Observed: I am puzzled as to why this error is occurring in the "basic query part" of Advanced Query, especially since the Basic Query, which contains same logic for argument2, runs correctly. I have reviewed the queries for any potential issues but have not been able to identify the cause of the problem. The java project under question is https://github.com/apache/shardingsphere-elasticjob. I am reaching out to the community for insights or suggestions on what might be causing this issue and how to resolve it. Any assistance or guidance would be greatly appreciated. Thank you in advance for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The full error says "...in type test" -- this is telling you that the type test A few other little hints while I'm here:
Here's the cleaned up version: import java
import semmle.code.java.dataflow.DataFlow
class AssertMethodCall extends MethodAccess {
AssertMethodCall() {
this.getMethod().getDeclaringType().getQualifiedName().matches("%Assert%") and
this.getEnclosingCallable() instanceof TestMethod
}
}
from
TestMethod tm, AssertMethodCall assertMethodCall, Callable focalM, Expr argument2,
MethodAccess focalMethodCall, VarAccess expectedVariable, Variable var, Expr varInit,
DataFlow::Node declaredNode, DataFlow::Node endNode
where
tm = assertMethodCall.getEnclosingCallable() and
focalMethodCall = assertMethodCall.getAnArgument() and
focalM = focalMethodCall.getCallee() and
focalM.fromSource() and
not focalM instanceof GetterMethod and
not focalM.hasNoParameters() and
argument2 = assertMethodCall.getAnArgument() and
argument2 != focalMethodCall and
(
expectedVariable = argument2
or
assertMethodCall.getMethod().getName().matches("assertThat%") and
argument2.(MethodAccess).getMethod().getName() = ["is", "instanceOf"] and
expectedVariable = argument2.(MethodAccess).getAnArgument()
) and
var = expectedVariable.getVariable() and
varInit = var.getAnAssignedValue() and
declaredNode = DataFlow::exprNode(varInit) and
DataFlow::localFlow(declaredNode, endNode) and
endNode.getLocation().getStartLine() < assertMethodCall.getLocation().getStartLine() and
endNode.getEnclosingCallable().getFile() = assertMethodCall.getEnclosingCallable().getFile()
select tm, assertMethodCall.getLocation(), expectedVariable, var,
endNode.asExpr().getEnclosingStmt() |
Beta Was this translation helpful? Give feedback.
The full error says "...in type test" -- this is telling you that the type test
not argument2 instanceof MethodAccess
is pointless. We can see why by noting we also have constraintsexpectedVariable = expected.(VarAccess)
andexpected = argument2
-- hence we know that for any result to be returned,expected
andargument2
must be aVarAccess
, andVarAccess
is disjoint withMethodAccess
, therefore the testnot argument2 instanceof MethodAccess
is redundant and can be deleted.A few other little hints while I'm here:
argument
is always cast toMethodAccess
, and is always equal tofocalMethodCall
, so we can dropfocalMethodCall = argument.(MethodAccess)
and just usefocalMethodCall
.not argu…