clion-cppcheck icon indicating copy to clipboard operation
clion-cppcheck copied to clipboard

cppcheck warns about unused functions (which are used!)

Open pisanuw opened this issue 6 years ago • 8 comments

Environment

  • Operating System (e.g. Ubuntu 16.04 x64): Darwin MAC-30223656 16.7.0 Darwin Kernel Version 16.7.0
  • IDE Version (e.g. CLion 2016.3.2): CLion 2018.2.3
  • Cppcheck executable version (cppcheck --version): Cppcheck 1.86
  • Cppcheck plugin version: 1.2.0
  • Exact strings used in cppcheck plugin options: --enable=all --force --inconclusive --language=c++ --std=c++14 --suppress=missingIncludeSystem
    • cppcheck path: /Users/pisan/xxx/bin/cppcheck
    • cppcheck options: ?

Expected behaviour

Do not warn about functions that are called

On command line cppcheck --enable=all --force --inconclusive --language=c++ --std=c++14 --suppress=missingIncludeSystem *.cpp does not give any warnings

Actual behaviour

cppcheck: (style) The function funcFoo is never used warning in CLion right gutter as well as "Code Inspection"

Steps to reproduce the behaviour

main.cpp

#include <iostream>

void funcFoo();

int main() {
  funcFoo();
  return 0;
}

foo.cpp

void funcFoo() {

}

pisanuw avatar Jan 16 '19 23:01 pisanuw

I also came across this. The command-line will not report the warning since it will scan all files in a single scan and see the usage. In the IDE it will only scan the separate file so it appear unused to it. I think it should use --suppress=unusedFunction for now to avoid this warnings.

I will also take a look at figuring out if this warning could be made a bit smarter - some ideas:

  • do not report as unused if only a single file is scanned and it is specified in the header
  • if a single file is used only report unused static functions
  • do not report unused functions at all when scanning a header

firewave avatar Mar 31 '20 13:03 firewave

I created a ticket with Cppcheck - see https://trac.cppcheck.net/ticket/9682

firewave avatar Apr 18 '20 22:04 firewave

There's a similar issue with unusedStructMember warnings in headers - see https://trac.cppcheck.net/ticket/9961

firewave avatar Nov 01 '20 11:11 firewave

Yes, this is why --enable=unusedFunction is not enabled by default in the plugin. Based on how this plugin runs (one file at a time), these warnings are not displayed correctly in the IDE.

This is causes by the --enable=all flag in your configuration.

johnthagen avatar Nov 01 '20 19:11 johnthagen

Still Cppcheck could be smarter about this. Tickets are filed and hopefully at some point someone will care about it... (possibly me at some point :D)

firewave avatar Nov 01 '20 20:11 firewave

Until Cppcheck behaves better I would suggest to internally disable these warnings for headers. I already have a patch ready.

firewave avatar Nov 20 '20 17:11 firewave

I filed another ticket with an idea on how to improve this - see https://trac.cppcheck.net/ticket/10727.

firewave avatar Jan 14 '22 16:01 firewave