Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 37 ยท Getting Started

Static Analysis Explained

Understand how static analysis finds bugs without running code: parsing, syntax trees, control flow and data flow, and why it differs from testing and from regex search.

Testing runs your program with chosen inputs and checks the output. Static analysis never runs it. It reads the code, builds a model and asks questions about every path through it, including paths your tests never reach. SonarQube is built on this idea, so it helps to know how it works and what it can and cannot promise.

Testing versus static analysis

A test proves that one scenario works. An analyzer can say that some scenario might fail. The two are complementary:

  • Tests check behavior against requirements. They cannot tell you a variable is never used.
  • Analyzers check code patterns across all paths. They cannot know what the program is supposed to do.

Static analysis also has a cost model that suits CI: it needs no environment, no database and no test data, and it takes seconds to minutes.

How a scanner reads your code

Source text Files as you wrote them.
Syntax tree The code as nested structure.
Flow graphs Every path the code can take.
Rules Patterns that flag an issue.

Each stage gives rules more power.

1 / 2

From text to tree

An analyzer first tokenizes the text (splits it into words, numbers and symbols) and then parses the tokens into a tree that mirrors the grammar of the language. if (a) { b(); } becomes an If node with a condition child and a body child. Rules are written against that tree, so formatting, comments and line breaks cannot confuse them.

That is the main reason a real analyzer beats a text search, as this stand-in demonstrates.

Python Text search versus syntax tree (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The text search flags the string on line 2, a false positive. The tree search flags only the call. False positives waste time and teach people to ignore the tool, which is why analyzer quality matters.

Control flow and data flow

A control flow graph lists the order in which statements can run, with an edge for every branch and loop. A data flow analysis then tracks where each value comes from and where it can go. Together they answer questions like:

  • Can this variable be null when it is dereferenced?
  • Is a resource opened on one branch but not closed on another?
  • Does user input reach a SQL query without being cleaned? (This is taint analysis, used for vulnerabilities.)

Here is a very small version: a checker that finds variables read before any assignment in straight-line code.

Python A mini flow check: use before assignment (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The stand-in is deliberately simple. A real engine handles loops, branches, function calls and many languages, and it works with path sensitivity: it knows that after if (x == null) return; the variable is no longer null.

Limits you should know

Static analysis cannot be perfect:

  • False positives: the tool reports a problem that is not one, usually because it cannot see a runtime fact.
  • False negatives: a real problem is not reported, for example because it depends on a value read from a file.
  • Context blindness: the tool does not know your business rules.

SonarQube handles these honestly. You can mark an issue as a false positive or accepted, and the decision is remembered. Rules also come with a description and compliant and non-compliant examples so you can judge each finding. Treat it as a very thorough reviewer, not as a judge.

Why it belongs in CI

Because analysis is cheap and deterministic, it runs on every commit. Problems are cheapest to fix at the moment they are written, and a pipeline that fails on new problems keeps them out. Continuous integration comes later in this track; for now, remember that the same code and the same rules produce the same results every time.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main difference between testing and static analysis?

    Static analysis examines code structure and paths without execution, so it complements but does not replace tests.
  2. 2. Why is a syntax tree better than a text search for rules?

    Rules on a tree match real code structure, so words inside strings or comments do not cause false positives.
  3. 3. What does taint analysis track?

    Taint analysis follows data from untrusted sources to sensitive sinks such as SQL queries to find vulnerabilities.
  4. 4. A tool reports a problem that is not real. What is this called?

    A false positive is a reported issue that is not an actual problem.

Summary

  • Static analysis reads code, builds a syntax tree and flow graphs, and applies rules.
  • Tree and flow rules are far more precise than text search.
  • Data flow and taint analysis find null dereferences, leaks and injection paths.
  • Expect some false positives and false negatives; review findings with judgment.
  • Cheap and deterministic, it fits into every CI run.

Next: Bugs, Vulnerabilities and Code Smells