heydeer

Static code analysis: how it works and the tools to use

· · 10 min read

Every team already runs some static code analysis, even if nobody calls it that: the compiler’s warnings, a linter in the editor, a type checker in CI. The question is which analyzers to add on top, what each one can actually find, and where a human or AI reviewer still has to take over.

Static code analysis is the automated examination of source code without running it. A static analyzer parses the code into a syntax tree, then applies rules, type checks and data-flow tracking to find bugs, security vulnerabilities and style problems. Tools range from linters such as ESLint and Ruff to SAST platforms such as SonarQube, Semgrep and CodeQL, and they complement dynamic analysis, which tests the program while it runs.

What static code analysis is

Static code analysis examines a program’s source (or compiled bytecode) without executing it. Because nothing runs, it needs no test data, no deployed environment and no inputs that happen to hit the bug. It looks at every branch, including the error handler nobody has ever triggered. That makes it fast, repeatable and cheap to run on every commit.

The same property sets its limits. An analyzer sees the code but not the running system: not the configuration, not the data, not what the author meant. OWASP’s summary of the trade-off holds up well. Static analysis scales to large codebases and is good at well-understood defects such as SQL injection and buffer overflows, but it produces false positives and struggles with authentication, access control and configuration problems that aren’t visible in the code.

How static analysis works

Almost every analyzer starts the same way. It parses the source into an abstract syntax tree (AST), a structured representation where db.execute(query) is a call node with a receiver and an argument rather than a string of characters. From there, tools differ in how far they go.

Pattern rules match shapes in the tree. That is how most linter rules work, and it is the core of Semgrep, whose rules look like the code they match:

rules:
  - id: python-eval
    pattern: eval(...)
    message: eval() runs arbitrary code; parse the input instead.
    languages: [python]
    severity: ERROR

Type and semantic analysis resolves names and types, so a rule can tell a string from a number or one execute method from another. Type checkers such as TypeScript’s compiler and mypy are static analyzers built entirely on this layer.

Data-flow and taint analysis follow values through the program. A taint rule names sources where untrusted data enters, sinks where it becomes dangerous and sanitizers that make it safe, then reports any path from a source to a sink that skips a sanitizer. In this Flask handler, the query parameter flows straight into SQL:

# app/search.py
from flask import request

def search(db):
    term = request.args.get("q")                  # source: user input
    query = f"SELECT * FROM items WHERE name = '{term}'"
    return db.execute(query)                      # sink: raw SQL
rules:
  - id: flask-sql-injection
    mode: taint
    pattern-sources:
      - pattern: request.args.get(...)
    pattern-sanitizers:
      - pattern: int(...)
    pattern-sinks:
      - pattern: $DB.execute(...)
    message: User input reaches a SQL query. Use a parameterized query.
    languages: [python]
    severity: ERROR

How far that tracking reaches is the main difference between tools. Semgrep Community Edition follows taint within a single function; its paid engine follows it across functions and files. CodeQL builds a database of the whole program and queries it, which is slower but finds flows that cross many files. The Clang Static Analyzer goes further still on individual paths, reasoning about the values variables can hold along each branch to find null dereferences and leaks.

Linters vs SAST vs type checkers

“Static analysis tool” covers three families that answer different questions, and most codebases need one of each:

  • Linters (ESLint, Ruff, clang-tidy, RuboCop, golangci-lint) check style, suspicious constructs and common mistakes, mostly one file at a time. They run in milliseconds to seconds, so they belong in the editor and in pre-commit hooks, usually with autofix.
  • Type checkers (tsc, mypy, Pyright, the compilers of typed languages) prove that values are used consistently. They catch a whole class of bugs, such as a possibly undefined value or a wrong argument type, with almost no false positives.
  • SAST tools (Semgrep, CodeQL, Snyk Code, the security rules in SonarQube) look for vulnerabilities, usually with taint tracking across functions and files. They take longer and run in CI or on a schedule, reporting into a dashboard or the pull request.

Code quality platforms such as SonarQube sit across the first and third, adding duplication, complexity, coverage and a quality gate that passes or fails a branch.

Static vs dynamic code analysis

Dynamic analysis is the other half: it watches the program run. Unit and integration tests are the everyday form, joined by fuzzing, memory and thread sanitizers, profilers and DAST scanners that attack a running web application.

AspectStatic analysisDynamic analysis
Runs the codeNo; reads source or bytecodeYes; needs a build, inputs and often an environment
WhenAs code is written: editor, hooks, every pull requestAfter it builds: test runs, staging, production monitoring
CoverageEvery path in the analyzed codeOnly the paths the inputs exercise
Typical findingsInjection flaws, null dereferences, type errors, unsafe APIs, styleCrashes, memory errors, race conditions, slow paths, runtime misconfiguration
False positivesSome, because it reasons about paths that may never runFew, since each finding happened; false negatives instead
ExamplesESLint, Ruff, SonarQube, Semgrep, CodeQLTest suites, AddressSanitizer, fuzzers, DAST scanners

Neither replaces the other. Static analysis is cheap and early but can’t observe behaviour; dynamic analysis observes behaviour but only for the inputs you thought to give it. A sanitizer-enabled test run catches the memory bug the analyzer missed, and the analyzer finds the injection in the code path no test reaches.

Static code analysis tools compared

The tools below cover the main families. Prices and free tiers are as of November 2026.

ToolWhat it doesLanguagesLicence and free tierWhere it runs
SonarQubeCode quality and security platform: bugs, code smells, coverage, quality gatesCommunity Build supports 21 languages; paid editions add moreCommunity Build free and open source; SonarQube Cloud Free for private projects up to 50,000 lines; Cloud Team from $34 a monthSelf-hosted server or SonarQube Cloud, fed by a scanner in CI; IDE plugin
SemgrepPattern and taint rules, mostly security (SAST)30+ languagesCommunity Edition is LGPL-2.1; the AppSec Platform is free for up to 10 contributors and 10 private repositoriesCLI, pre-commit, CI, IDE; cross-file analysis in the paid engine
CodeQL (GitHub code scanning)Queries over a database built from the code; deep data-flow security analysisC/C++, C#, Go, Java/Kotlin, JavaScript/TypeScript, Python, Ruby, Rust, Swift, GitHub ActionsFree on public repositories; private repositories need GitHub Code Security at $30 per active committer a monthGitHub Actions (default or advanced setup), or the CodeQL CLI in other CI
Snyk CodeSAST inside the Snyk platform, next to dependency and container scanningMainstream web, mobile and backend languagesFree plan includes 100 Snyk Code tests a month; paid plans aboveIDE, CLI, repository integrations and CI
ESLintLinter with plugins for frameworks, TypeScript and security rulesJavaScript and TypeScript (with typescript-eslint)Open source (MIT), freeEditor, pre-commit, CI; needs Node.js ^20.19.0, ^22.13.0 or 24+
RuffLinter and formatter written in Rust, 900+ rulesPythonOpen source (MIT), freeEditor, pre-commit, CI
Clang Static Analyzer and clang-tidyPath-based bug finding (analyzer) and a lint/modernization tool (clang-tidy)C, C++, Objective-COpen source, part of the LLVM/Clang projectscan-build, Xcode, clang-tidy in editors and CI
PMDRule-based analyzer plus CPD, a copy-paste detectorMainly Java and Apex, about 16 others; CPD covers moreOpen source (BSD-style), freeCLI, Maven and Gradle plugins, CI
SpotBugsBug-pattern detector, the successor to FindBugs; 400+ patternsJava (compiled code from any Java version; runs on Java 11+)Open source (LGPL), freeCLI, Maven and Gradle plugins, IDE, CI

SonarQube is the broadest: one dashboard for bugs, code smells, duplication, coverage and security hotspots, with a quality gate that can block a merge. Teams that mostly use it for security rules, or that find its pricing by lines of code awkward, compare it with the options in SonarQube alternatives.

Semgrep is the easiest SAST tool to customize, since a rule is a YAML file that looks like the code it matches. semgrep scan runs the Community Edition with community rules for the languages it finds. CodeQL powers GitHub code scanning; default setup turns it on from repository settings without a workflow file, and its alerts appear on pull requests. Snyk Code fits teams already using Snyk for dependencies.

Language-specific analyzers do the daily work. ESLint and Ruff run in the editor and on each commit. clang-tidy’s check groups (bugprone-, clang-analyzer-, modernize-, performance-, readability-) cover C++ from bugs to style, and -fix applies the fixes it supports. PMD and SpotBugs remain the standard pair for Java, one reading source and the other checking compiled classes for more than 400 bug patterns. A typical set of commands:

# JavaScript / TypeScript
npm init @eslint/config@latest   # creates eslint.config.js
npx eslint .

# Python
ruff check --fix
mypy src/

# Many languages, security rules
semgrep scan                     # Community Edition with community rules

# C and C++
clang-tidy src/parser.cpp -checks=-*,bugprone-*,clang-analyzer-*
scan-build make                  # Clang Static Analyzer over a build

Strengths and limits of static analysis

The strengths are hard to match. An analyzer gives the same answer every time, checks every path, runs in seconds to minutes and enforces a rule across a million lines as easily as across ten. For a known class of defect, such as an unparameterized query, that consistency beats any reviewer.

The limits come from the same design:

  • False positives. Deciding in general what a program will do is undecidable, so analyzers approximate. Approximating safely means flagging paths that can’t actually happen, and a noisy analyzer trains people to suppress warnings without reading them. Tuning rule sets and baselining existing findings is ongoing work.
  • False negatives outside the rules. An analyzer finds what its rules describe. A sanitizer the rule doesn’t know about, a framework it doesn’t model or a flow through configuration all fall outside.
  • No idea of intent. A function that charges a customer twice, a permissions check applied to the wrong role or a migration that drops the wrong column can all be clean, well-typed code. Nothing in the source says it’s wrong; only the purpose of the change does.
  • Cross-file logic. Renaming a field, changing what a function returns or adding a required argument can break callers elsewhere in ways a type checker catches only when the types change too.

Where AI code review fits alongside static analysis

Those last two limits are where review comes in, and where AI code review earns its place next to the analyzers rather than instead of them. An analyzer applies fixed rules to code; an AI reviewer reads the change together with its description and the code around it, and asks whether the change does what it says. The AI code review guide goes through what that catches and what it still misses.

HeyDeer is an AI code reviewer for GitHub pull requests. It reads what a change is meant to do and checks whether the code does it, including how it affects code in other files, and verifies each finding against the code before reporting it. The same review runs before a pull request exists with the HeyDeer CLI: heydeer review "<what the change is meant to do>". Keep the linters in the editor and the SAST scan in CI, and add HeyDeer on each pull request for the logic and intent that rules don’t describe.

Sources

  1. OWASP: Static Code Analysis
  2. SonarQube Community Build
  3. Sonar plans and pricing
  4. semgrep/semgrep: README
  5. Semgrep docs: Quickstart (CE)
  6. Semgrep docs: Taint analysis
  7. Semgrep pricing
  8. GitHub Docs: About code scanning with CodeQL
  9. GitHub Docs: About GitHub Advanced Security
  10. GitHub Security plans and pricing
  11. ESLint: Getting started
  12. Ruff documentation
  13. astral-sh/ruff: README
  14. Clang Static Analyzer
  15. clang-tidy documentation
  16. PMD
  17. SpotBugs
  18. Snyk plans

Frequently asked questions

What is static code analysis?

Static code analysis is checking source code for defects without executing it. Analyzers parse the code, then match patterns, check types and follow data through the program to report bugs, vulnerabilities and maintainability problems. It runs in editors, git hooks and CI, and covers every code path, including ones tests never reach.

Is SonarQube static code analysis?

SonarQube is a static code analysis platform. It analyzes source code without running it for bugs, code smells, security vulnerabilities and duplication, and adds dashboards, coverage import and quality gates that pass or fail a branch. Its Community Build is free and open source, and SonarQube Cloud has a free plan for private projects up to 50,000 lines of code.

Is Semgrep free to use?

Semgrep is free in two ways. Semgrep Community Edition, the open source engine, is licensed under LGPL-2.1 and runs with community rules from the command line, though it analyzes within a single function or file. The Semgrep AppSec Platform, which adds cross-file analysis, is free for up to 10 contributors and 10 private repositories (as of November 2026).

What is the Clang Static Analyzer?

The Clang Static Analyzer is an open source tool from the LLVM/Clang project that finds bugs in C, C++ and Objective-C programs, such as null dereferences, memory leaks and use of uninitialized values, by reasoning about paths through the code. It runs through scan-build, inside Xcode, or as the clang-analyzer checks in clang-tidy, and is slower than compiling.

What is the difference between static and dynamic code analysis?

Static code analysis inspects source code without running it, so it can examine every path early and cheaply but reports some false positives. Dynamic code analysis observes the program while it runs, through tests, fuzzing, sanitizers or DAST scanners, so its findings are real but limited to the paths that were executed. Most teams use both.

Is a linter a static code analysis tool?

A linter is a static code analysis tool focused on fast, local rules: style, suspicious constructs, unused code and common mistakes in one file at a time. SAST tools such as Semgrep and CodeQL are also static analyzers but track data across functions and files to find security vulnerabilities, which takes longer and usually runs in CI.

Can AI code review replace static analysis?

AI code review works best alongside static analysis rather than in place of it. Static analyzers give the same deterministic answer on every run and enforce rules at scale, while AI review reads the change against its description and the surrounding code to catch logic errors and broken callers that rules don’t describe. Most teams run analyzers in CI and AI review on each pull request.

Start with your first PR.

Free plan. No credit card required.

Get started free