c:S5536 Unused Functions in Shared Object File

Not entirely sure if this is a false positive or not for c:S5536 - Functions that are not used in a project should be removed - because the description seems misleading to me.

Running SonarQube 9.8.0.63668, build-wrapper, version 6.40 (linux-x86), SonarScanner 4.7.0.2747 - when building dynamic libraries (.so files) and scanning with SonarQube, the functions meant to be used externally are marked by c:S5536 as dead code which needs to be removed. The rule description states “Unless you are in a library codebase context,” - in which I am so I would expect the rule not to trigger.

For example:

#include <stdio.h>

int unused_in_lib_but_used_from_outside() {
        printf("This will just print\n");
        return 0;
}

Compile with gcc -fPIC -shared -o test_so_unused.so test_so_unused.c - SonarQube will trigger the rule and ask that unused_in_lib_but_used_from_outside be removed.

Hi @Adrianh,

This part of the rule description just says that if you are analyzing a library (and the library only, see later), you can ignore this rule by removing it from your quality profile.

Basically, when analyzing library code (as opposed to analyzing a full application, that may contain some libraries), there are 3 situations:

  • You use a library that you don’t own, and it gets analyzed. This is usually not what you should be doing. You can read Narrowing the focus with an analysis scope for more info about how you could fix this.
  • You are analyzing a library that you own, along with its unit tests. In that case, there should be no unused function, all functions should be used directly or indirectly from the tests.
  • You analyze a library that you own, but without unit tests. In that case, you should probably deactivate this rule which no longer makes sense.

Since we cannot guess your situation, you have to make the call if you want to keep this rule or not.

Hope this helps!

Thank you, that is helpful. I will look into the option of integrating unit tests for better coverage.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.