1

Say, I have a bunch of .cc source files with statements like

using namespace lib::a;
using namespace lib::d;
using namespace lib::c;
using namespace lib::b;

I want them to be sorted

using namespace lib::a;
using namespace lib::b;
using namespace lib::c;
using namespace lib::d;

using a .clang-format file.

Is there a key value pair to achieve this? I know that this works for header files, but I have not seen it for sorting namespaces.

Simon
  • 325
  • 1
  • 6
  • 1
    I am sure you have a good reason to do so, but I would say that using entire namespaces is not a good practice, or at least you have to be very careful about, i.e. not putting many of these in bulk. – alfC Aug 18 '23 at 08:24
  • I actually thought it is common practice to use namespaces in source files, but not in header files? – Simon Aug 18 '23 at 08:37
  • It is less of a problem in source files, but still quite bad IMO. – alfC Aug 18 '23 at 09:36

1 Answers1

3

For current (latest) clang-format version the answer is: Unfortunately, No.
Although it has great "include" sorting features, there is no key for "using"s.

If the feature is added, it will be mentioned in the Clang-format Documentation

And also I don't recommend using "using namespace". As it is dangerous and is considered as bad practice.
See the warnings:

  1. Topic 1
  2. Topic 2
sens
  • 71
  • 3
  • Sorting `using` declarations is not easy/good practice. Just consider dependent names; clearly inner namspace should not precede its nesting namespace. But if you are using unqualified names( the whole point of `using mamespace`), what is the formatter supposed to do? Should it preprocess the code to check the possibility of reordering, or just reorder and let compilation fail after the change? – Red.Wave Aug 18 '23 at 09:02
  • Some namespace are intended to be "used" that way: those which bring `operator`s (`std::litterals` for example). – Jarod42 Aug 18 '23 at 11:09