A stray semicolon is no longer detected by pedantic modern compilers because the C and C++ standards explicitly allow an empty statement, and modern compilers have moved away from warning about constructs that are legal and often intentional. What was once a common warning—often enabled by flags like -Wempty-body or -Wextra—has been largely silenced or reclassified as a stylistic issue rather than a probable error.
The Historical Role of the Semicolon Warning
In early C compilers, a semicolon after a control statement (e.g., if (x);) was frequently a typo that changed program logic silently. Compilers like GCC and Clang introduced warnings such as -Wempty-body to flag suspicious empty bodies. However, these warnings were never part of the language standard; they were quality-of-implementation features. Over time, as codebases grew and idioms like while (read()); became recognized as intentional, compiler vendors adjusted their default warning sets to reduce noise.
Language Standards and the Empty Statement
Both C (since C89/C90) and C++ (since C++98) define the null statement as a semicolon alone. It is a valid statement that does nothing. The standards do not require a diagnostic for it. Pedantic mode (-pedantic in GCC/Clang) checks for conformance to the standard, not for style. Since an empty statement is conforming, pedantic mode does not warn. Warnings about empty bodies are separate, optional flags that are not enabled by -pedantic or -Wall in many modern toolchains.
How Compiler Warning Policies Have Shifted
Compiler developers have increasingly focused on actionable warnings—those that indicate likely bugs—rather than stylistic preferences. The empty statement is often used deliberately in macros, loops, and synchronization primitives. For example, a spinlock might be implemented as while (lock_test_and_set());. Warning on every such construct would be counterproductive. Consequently, warnings like -Wempty-body are now off by default in GCC and Clang, and even when enabled, they may not trigger for all cases (e.g., a semicolon on its own line after a brace).
Common Cases Where Stray Semicolons Appear
- After a control statement:
if (condition);— often a mistake, but legal. - Inside macros:
#define DO_NOTHING() ;— intentional no-op. - In loops:
while (condition);— busy-wait or intentional empty loop. - After a class or struct definition in C++:
struct S {};;— extra semicolon is harmless. - At the end of a block:
{ int x = 1; };— empty statement after block.
Why Pedantic Mode Does Not Flag Them
Pedantic mode in GCC and Clang (-pedantic or -pedantic-errors) issues diagnostics only for violations of the language standard. An empty statement is explicitly permitted, so no diagnostic is required. Some compilers may have historically issued warnings under -pedantic for certain extensions, but not for standard constructs. The warning about empty bodies is controlled by -Wempty-body, which is not part of pedantic mode. In fact, GCC documentation states that -Wempty-body is enabled by -Wextra but not by -Wall or -pedantic.
How to Re-enable Detection If Desired
If you want to catch stray semicolons, you can explicitly enable the relevant warnings. In GCC and Clang, use -Wempty-body (often included in -Wextra). For C++, -Wextra-semi warns about extra semicolons after member function definitions or class definitions. Some static analysis tools (e.g., clang-tidy) have checks like readability-extra-semicolon or bugprone-suspicious-semicolon that can flag suspicious cases. However, these are opt-in and not part of pedantic mode.
Frequently Asked Questions
Does -pedantic ever warn about semicolons?
No, -pedantic only warns about non-standard extensions or violations of the standard. An empty statement is standard, so it is not flagged.
Why did my old compiler warn about if (x); but my new one doesn't?
Compiler vendors have changed default warning sets to reduce noise. The warning -Wempty-body is no longer enabled by default in many configurations, and it was never part of -pedantic.
Is a stray semicolon after a function definition an error?
In C, a semicolon after a function definition is a syntax error. In C++, it is allowed (an empty declaration). Pedantic mode may warn in C but not in C++.
How can I make my compiler warn about empty statements?
Use -Wempty-body for GCC/Clang, or -Wextra-semi for C++ extra semicolons. For static analysis, consider clang-tidy checks.
Are there cases where a stray semicolon is actually a bug?
Yes, a semicolon after an if or for statement can unintentionally create an empty body, changing control flow. That is why some projects enable -Wempty-body explicitly.
Alternatives for Catching Suspicious Semicolons
Beyond compiler warnings, linters and static analyzers can help. Clang-tidy offers bugprone-suspicious-semicolon, which flags semicolons that are likely mistakes (e.g., after if without braces). PVS-Studio and other commercial tools have similar checks. Code review and consistent formatting also reduce the chance of stray semicolons. Some style guides recommend always using braces for control statements, which makes accidental empty bodies more visible.