Skip to content

Static Analysis

In one line

Static analysis scans code without running it — finds type errors, bugs, security issues, and style violations automatically.

In simple words

Static analysis examines your source code without running it. It catches issues that can be detected from the code structure alone — before tests, before deployment, before production.

What static analysis catches:

  1. Type errors — wrong types passed to functions, null dereference
  2. Unused code — dead variables, unreachable branches, unused imports
  3. Style violations — naming conventions, formatting, complexity
  4. Security vulnerabilities — known dangerous patterns (SQL injection, XSS)
  5. Bug patterns — common mistakes (off-by-one, comparison errors)
  6. Code smells — long functions, high complexity, duplication
  7. Dependency issues — vulnerable packages, license conflicts

Types of static analysis:

  1. Linters — style and common mistakes (ESLint, pylint, golangci-lint)
  2. Type checkers — type system enforcement (TypeScript, mypy, Flow)
  3. Security scanners — vulnerability detection (Semgrep, CodeQL, Bandit)
  4. Complexity analyzers — code metrics (cyclomatic complexity, coupling)

Static analysis is fast (seconds), deterministic, and catches issues early. It should run on every commit in CI/CD. It complements (but doesn't replace) tests and code reviews.

Example

javascript
// ESLint configuration (.eslintrc.js)
module.exports = {
  rules: {
    'no-unused-vars': 'error',        // catch dead code
    'no-undef': 'error',              // catch undefined variables
    'eqeqeq': 'error',                // require === instead of ==
    'no-console': 'warn',             // warn on console.log
    'complexity': ['error', 10],      // max cyclomatic complexity
    'max-depth': ['error', 4],        // max nesting depth
    'max-lines-per-function': ['error', 100],
  },
};

// TypeScript catches type errors:
// function add(a: number, b: number): number { return a + b; }
// add(1, '2'); // ERROR: Argument of type 'string' is not
//              // assignable to parameter of type 'number'.

// Semgrep catches security patterns:
// const query = "SELECT * FROM users WHERE name = '" + input + "'";
// // ↑ Semgrep: "Detected string concatenation in SQL query.
// //    This is vulnerable to SQL injection. Use parameterized queries."

// Run in CI: eslint . && tsc --noEmit && semgrep scan

Static analysis tools (ESLint, TypeScript, Semgrep) scan code without running it. They catch type errors, bugs, security issues, and style violations automatically — in seconds, on every commit.

Common confusions

  • Confused with: code review

    The difference: Static analysis is automated tool analysis — it catches deterministic issues (types, style, known patterns) but can't understand intent or design. Code review is human peer review — it catches design issues, logic errors, and shares knowledge, but is subjective and limited by attention. Static analysis is fast and consistent; code review is nuanced and educational. Use both.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary