Skip to content

Instantly share code, notes, and snippets.

@erik-krogh
Created December 20, 2021 22:46
Show Gist options
  • Save erik-krogh/b31a6afa616df3a93420907acd54978a to your computer and use it in GitHub Desktop.
Save erik-krogh/b31a6afa616df3a93420907acd54978a to your computer and use it in GitHub Desktop.
/**
* @name Using exists instead of a cast
* @description TODO
* @kind problem
* @problem.severity warning
* @id ql/exists-instead-of-cast
* @precision high
*/
import ql
from Exists ex, ComparisonFormula comp, AstNode operand, AstNode formula
where
count(ex.getArgument(_)) = 1 and
exists(ex.getRange()) and
exists(ex.getFormula()) and
comp = ex.getRange() and
comp.getOperator() = "=" and
comp.getAnOperand().(VarAccess).getDeclaration() = ex.getArgument(0) and
not operand.(VarAccess).getDeclaration() = ex.getArgument(0) and
operand = comp.getAnOperand() and
count(VarAccess access | access.getDeclaration() = ex.getArgument(0)) = 2 and
formula = ex.getFormula()
select ex, "This exists can be simplfied to a cast of $@ to " + ex.getArgument(0).getType().getName() + ".", operand, "the operand"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment