Skip to content

[clang-format] Qualifiers are not sorted properly when [[maybe_unused]] or [[nodiscard]] attributes are used #123573

@lluisalemanypuig

Description

@lluisalemanypuig

I'm using clang-format 19

$ clang-format-19 --version
Ubuntu clang-format version 19.1.7 (++20250114103320+cd708029e0b2-1~exp1~20250114103432.75)

to format my C++ code. I specified the order of qualifiers in the following manner in my .clang-format file:

Language: Cpp
QualifierAlignment: Custom
QualifierOrder: [static, constexpr, inline, const, friend, type]

and it works well for functions and variable declarations when there are no attributes at the front, but it fails to sort them properly when there are qualifiers such as [[nodiscard]] or [[maybe_unused]]:

// the [[nodiscard]] attribute prevents clang-format from
// ordering the qualifiers 'constexpr' and 'static' properly.
[[nodiscard]] inline constexpr static int function_with_nodiscard() noexcept {
  return 1;
}
// the attributes are ordered according to the specified order in the
// .clang-format file
static constexpr inline int function_without_nodiscard() noexcept { return 1; }

int main() {
  // the [[maybe_unused]] attribute prevents clang-format from
  // ordering the qualifiers 'constexpr' and 'static' properly.
  [[maybe_unused]] constexpr static int A = 1234;
  // the attributes are ordered according to the specified order in the
  // .clang-format file
  static constexpr int B = 1234;
}

I expected all the qualifiers to be ordered according to the predefined order in the file, so that they are ordered like this:

[[nodiscard]] static constexpr inline int function_with_nodiscard() noexcept {
  return 1;
}
static constexpr inline int function_without_nodiscard() noexcept { return 1; }

int main() {
  [[maybe_unused]] static constexpr int A = 1234;
  static constexpr int B = 1234;
}

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions