Convert to C++17 nested namespaces in clang-format?

Viewed 1190

I am trying to enable clang-format for the first time in a codebase. We are already using clang-tidy to check for code smells, and it has started warning us on code like this:

namespace foo { namespace bar {

or

namespace foo {
    namespace bar {

with the modernize-concat-nested-namespaces check. It wants to see

namespace foo::bar {

I was hopeful that running clang-format could fix this for me, but no dice. Is there a way to do this with clang-format?

1 Answers

I don't want to steal @Barry's answer (from comment on the original question), but it's been 4 months... It is true that clang-tidy can fix this for you, and clang-format mostly does whitespace and won't fix this for you.

For clang-tidy, you would do this:

clang-tidy -checks='-*,modernize-concat-nested-namespaces' -fix myfile.cpp

Explanation:

  • The -* disables all checks.
  • The subsequent modernize-concat-nested-namespaces enables that check.
  • The -fix tells clang-tidy to fix any issues found, but only if there are no compilation errors. To force it to fix the issue even if there are compilation errors, use -fix-errors.

You can specify multiple source files on the command-line if you want to.

Related