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:
- Type errors — wrong types passed to functions, null dereference
- Unused code — dead variables, unreachable branches, unused imports
- Style violations — naming conventions, formatting, complexity
- Security vulnerabilities — known dangerous patterns (SQL injection, XSS)
- Bug patterns — common mistakes (off-by-one, comparison errors)
- Code smells — long functions, high complexity, duplication
- Dependency issues — vulnerable packages, license conflicts
Types of static analysis:
- Linters — style and common mistakes (ESLint, pylint, golangci-lint)
- Type checkers — type system enforcement (TypeScript, mypy, Flow)
- Security scanners — vulnerability detection (Semgrep, CodeQL, Bandit)
- 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
// 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 scanStatic 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
- Code Quality
Static analysis.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.