Function has an empty body
ID |
c.maintainability.empty_function_body |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
C / C++ |
Description
Reports a function definition whose body contains no statements at all — an empty pair of braces, whether written on one line or spread across several with only whitespace or comments inside. Declarations without a body (a prototype ending in ;, a pure virtual = 0;) are not reported, since they have no body to leave empty.
Rationale
An empty body gives a reader no way to tell an unfinished stub from a deliberate decision to do nothing, and both possibilities are wrong in a different way. A stub that was never filled in accepts every call silently and does none of the work its name promises, so the missing effect surfaces as absent output somewhere else entirely, far from this function. A body that is intentionally empty (a no-op callback, a default override with nothing to add) is legitimate, but with no comment or logging to say so it reads exactly like the unfinished case, and the next reader cannot tell which one they are looking at.
The following code illustrates the pattern detected by this rule:
// FLAGGED: Function has an empty body
void noop(void) { }
Remediation
Implement the function, or delete it together with the call sites that reach it if the work turns out not to be needed — an unused empty function is dead code that reads as a feature. When doing nothing really is the correct behavior, make that explicit and searchable: add a comment stating why the function is intentionally empty, or log the skipped operation, rather than leaving a bare { } that looks identical to an unfinished stub.