CodeQL documentation

Unused static function

ID: cpp/unused-static-function
Kind: problem
Severity: recommendation
Precision: high
Tags:
   - efficiency
   - useless-code
   - external/cwe/cwe-561
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds static functions with definitions that are never called or accessed. These unused functions should be removed to increase code readability, reduce object size and avoid misuse.

Recommendation

Removing these unused static functions will make code more readable. A common pitfall is that code using a static function is guarded by conditional compilation but the static function is not. Notice that this detects directly unused functions and removing a static function may expose more unused functions.

Example

//start of file
static void f() { //static function f() is unused in the file
    //...
}
static void g() {
    //...
}
void public_func() { //non-static function public_func is not called in file, 
                     //but could be visible in other files
    //...
    g(); //call to g()
    //...
}
//end of file

References