Ruby: Ssa::WriteDefinition::getWriteAccess should return a CFG node

This commit is contained in:
Tom Hvitved
2023-03-16 11:13:13 +01:00
parent 720eed398b
commit 1d0b3d4112
11 changed files with 214 additions and 156 deletions

View File

@@ -11,6 +11,7 @@
// This is an implementation of the Rubocop rule
// https://github.com/rubocop/rubocop-performance/blob/master/lib/rubocop/cop/performance/detect.rb
import codeql.ruby.AST
import codeql.ruby.CFG
import codeql.ruby.dataflow.SSA
/** A call that extracts the first or last element of a list. */
@@ -41,12 +42,12 @@ class EndCall extends MethodCall {
}
Expr getUniqueRead(Expr e) {
exists(AssignExpr ae |
e = ae.getRightOperand() and
forex(Ssa::WriteDefinition def | def.getWriteAccess() = ae.getLeftOperand() |
forex(CfgNode eNode | eNode.getNode() = e |
exists(Ssa::WriteDefinition def |
def.assigns(eNode) and
strictcount(def.getARead()) = 1 and
not def = any(Ssa::PhiNode phi).getAnInput() and
def.getARead() = result.getAControlFlowNode()
def.getARead().getNode() = result
)
)
}

View File

@@ -48,7 +48,7 @@ newtype DefLoc =
/** A local variable. */
LocalVariableLoc(VariableReadAccess read, VariableWriteAccess write) {
exists(Ssa::WriteDefinition w |
write = w.getWriteAccess() and
write = w.getWriteAccess().getNode() and
read = w.getARead().getExpr() and
not read.isSynthesized()
)

View File

@@ -24,5 +24,5 @@ from RelevantLocalVariableWriteAccess write, LocalVariable v
where
v = write.getVariable() and
exists(write.getAControlFlowNode()) and
not exists(Ssa::WriteDefinition def | def.getWriteAccess() = write)
not exists(Ssa::WriteDefinition def | def.getWriteAccess().getNode() = write)
select write, "This assignment to $@ is useless, since its value is never read.", v, v.getName()

View File

@@ -23,5 +23,5 @@ class RelevantParameterVariable extends LocalVariable {
}
from RelevantParameterVariable v
where not exists(Ssa::WriteDefinition def | def.getWriteAccess() = v.getDefiningAccess())
where not exists(Ssa::WriteDefinition def | def.getWriteAccess().getNode() = v.getDefiningAccess())
select v, "The parameter '" + v.getName() + "' is never used."