Skip to content

Commit

Permalink
Add CodeQL query to detect redundant assignments
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Yao <[email protected]>
  • Loading branch information
ryao committed Jan 22, 2024
1 parent ac944f0 commit a5d9d01
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/codeql-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: "Custom CodeQL Analysis"

queries:
- uses: ./.github/codeql/custom-queries/cpp/redundantAssignment.ql
4 changes: 4 additions & 0 deletions .github/codeql-python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: "Custom CodeQL Analysis"

paths-ignore:
- tests
8 changes: 8 additions & 0 deletions .github/codeql/custom-queries/cpp/qlpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: cpp-custom-queries
version: 0.0.1
library: false
groups:
- cpp
- examples
dependencies:
codeql/cpp-all: ${workspace}
18 changes: 18 additions & 0 deletions .github/codeql/custom-queries/cpp/redundantAssignment.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import cpp

from AssignExpr firstAssign, AssignExpr secondAssign
where
// The first assignment is from 'a' to 'b'
firstAssign.getDest().(VarAccess).getTarget() = secondAssign.getSource().(VarAccess).getTarget() and
firstAssign.getSource().(VarAccess).getTarget() = secondAssign.getDest().(VarAccess).getTarget() and
// Ensure 'a' and 'b' are not modified in between these assignments
not exists(Expr anyExpr |
anyExpr.getEnclosingFunction() = firstAssign.getEnclosingFunction() and
anyExpr.isBetween(firstAssign, secondAssign) and
(
anyExpr.(AssignExpr).getDest().(VarAccess).getTarget() = firstAssign.getDest().(VarAccess).getTarget() or
anyExpr.(AssignExpr).getDest().(VarAccess).getTarget() = firstAssign.getSource().(VarAccess).getTarget()
)
)
select secondAssign, "This assignment is redundant."

1 change: 1 addition & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
config-file: .github/codeql-${{ matrix.language }}.yml
languages: ${{ matrix.language }}

- name: Autobuild
Expand Down

0 comments on commit a5d9d01

Please sign in to comment.