CLion randomly including <utility>

Answered

CLion keeps including the <utility> header, seemingly at random. I think it might be linked to when I use the Alt + Enter shortcut to include some required header (completely unrelated with <utility>, for example including <memory> or <vector>). But I cannot identify any pattern.

<utility> is never actually offered by the Alt + Enter shortcut, it is simply added at some point for no reason in the include directives.

Sometimes it is quite degenerate and it will include <utility> multiple times, and it will keep growing. It is the case for some extremely trivial files (very small header files etc.) as well as more fleshed out classes, not pattern either here.

1
3 comments

Hi Quentin!

What CLion version do you use? The problem sounds like https://youtrack.jetbrains.com/issue/CPP-15972 which should be fixed in CLion 2019.2.

1

Hello, thanks for your quick reply!

Yes, it seems to be the exact same issue! I am actually on 2019.2 now (but did not perform 2019.2.1 update yet).

Nevertheless, I did the test with the exact same example posted in the issue you linked:

<utility> is indeed only included once, not several times in this example.

I think that solves the issue.

I am not really sure if this behavior is consistent with other headers, in this case <utility> is somehow included (because std::move is defined in <utility> I guess) but including the header is not required since it was already provided.

Minimal example to reproduce the issue:

FIRST STEP, then SECOND STEP (Accept Clang-Tidy suggestion)

#include <string>

class Object {
public:
explicit Object(std::string data) : data(data) {};
private:
std::string data;
};

int main() {

}
#include <string>
#include <utility> // Not required but included

class Object {
public:
explicit Object(std::string data) : data(std::move(data)) {};
private:
std::string data;
};

int main() {

}
0

Thank you for the example! I've added it in the comments to https://youtrack.jetbrains.com/issue/CPP-13082. Feel free to comment or upvote the issue in order to get updates.

0

Please sign in to leave a comment.