How to enable checking of unused includes ?
-
Dear all,
qtCreator is a great tool, but I am missing some features that are natively enabled in IDEs specialized in Java or Python.
For instance, in the code below, obviously the header
threadis not used, but I couldn't enable this warning.#include <iostream> #include <thread> int main() { std::cout << "Hello World!" << std::endl; return 0; }In longer codes that warning can be useful.
-
Dear all,
qtCreator is a great tool, but I am missing some features that are natively enabled in IDEs specialized in Java or Python.
For instance, in the code below, obviously the header
threadis not used, but I couldn't enable this warning.#include <iostream> #include <thread> int main() { std::cout << "Hello World!" << std::endl; return 0; }In longer codes that warning can be useful.
@fernandesh
It will be interesting to see if the C++ experts know better, but I don't think this could be done for C++.#includefiles can contain all sorts of stuff, and preprocessor directives, I would have thought it would be very difficult to be sure that nothing in an included had any effect anywhere. Java or Python are more organized/restrictive on imports. I do not recall seeing any IDE for C++ showing me included files were not needed.Now someone will tell me I'm quite wrong.... :)
-
@fernandesh
It will be interesting to see if the C++ experts know better, but I don't think this could be done for C++.#includefiles can contain all sorts of stuff, and preprocessor directives, I would have thought it would be very difficult to be sure that nothing in an included had any effect anywhere. Java or Python are more organized/restrictive on imports. I do not recall seeing any IDE for C++ showing me included files were not needed.Now someone will tell me I'm quite wrong.... :)
@JonB You're correct.
There are some external tools like e.g. https://github.com/include-what-you-use/include-what-you-use -
@fernandesh
It will be interesting to see if the C++ experts know better, but I don't think this could be done for C++.#includefiles can contain all sorts of stuff, and preprocessor directives, I would have thought it would be very difficult to be sure that nothing in an included had any effect anywhere. Java or Python are more organized/restrictive on imports. I do not recall seeing any IDE for C++ showing me included files were not needed.Now someone will tell me I'm quite wrong.... :)
@JonB, thanks for your enlightening answer.
What you pointed was one of my concerns, too: for instance, defines can be anywhere.
However, in a world of precompiled headers, maybe there's light to this possible (and sometimes probable) mess.
And someday the C++ modules (defined in C++20) will become a reality and we C++ programmers will no longer be envious of Python and Java.
-
@fernandesh
It will be interesting to see if the C++ experts know better, but I don't think this could be done for C++.#includefiles can contain all sorts of stuff, and preprocessor directives, I would have thought it would be very difficult to be sure that nothing in an included had any effect anywhere. Java or Python are more organized/restrictive on imports. I do not recall seeing any IDE for C++ showing me included files were not needed.Now someone will tell me I'm quite wrong.... :)
-
@JonB Qt Creator 12 has this feature now. It highlights unused includes, but not all of them are correct.
@JoeCFD said in How to enable checking of unused includes ?:
but not all of them are correct.
Which is my point! I would have thought it might give false positives (in the sense of thinking an include had no effect when it actually does). Might be pathological cases, I don't know.
-
@JonB You're correct.
There are some external tools like e.g. https://github.com/include-what-you-use/include-what-you-useThanks for your reference @Christian-Ehrlicher !
include-what-you-wantlooks promising, and is project constantly updated. I'll give it a try. -
@JoeCFD said in How to enable checking of unused includes ?:
but not all of them are correct.
Which is my point! I would have thought it might give false positives (in the sense of thinking an include had no effect when it actually does). Might be pathological cases, I don't know.
-
It mostly depends on how close to the compiler is the editor's code model. Those have to take some shortcuts for the sake of speed, but they are getting pretty good.
I don't know about Qt Creator, but I've used it in Visual Studio and Rider and both work pretty much as expected. The few false positives are usually a simple compilation error because of a missing type, so an easy fix. Of course you can always f up your project if you use a bunch of conditionals in the preprocessor, but that's always a risk even without the feature.
-
It mostly depends on how close to the compiler is the editor's code model. Those have to take some shortcuts for the sake of speed, but they are getting pretty good.
I don't know about Qt Creator, but I've used it in Visual Studio and Rider and both work pretty much as expected. The few false positives are usually a simple compilation error because of a missing type, so an easy fix. Of course you can always f up your project if you use a bunch of conditionals in the preprocessor, but that's always a risk even without the feature.
As @Chris-Kawa and @JoeCFD already said, Creator now has this feature.
It is provided by Clangd, and therefore as good as Clangd understands your project.
In header files, I got some false positives also, but in source files it works quite good.
Regards