admin管理员组

文章数量:1123407

My application is written in C99 (or rather GNU99, and I wouldn't mind going to GNU11), and I'd like to require trailing commas in structure initializers. Does GCC have a warning (turned into error through -Werror) that I could enable? I couldn't find one.

My application is written in C99 (or rather GNU99, and I wouldn't mind going to GNU11), and I'd like to require trailing commas in structure initializers. Does GCC have a warning (turned into error through -Werror) that I could enable? I couldn't find one.

Share Improve this question edited 15 hours ago ks1322 35.6k14 gold badges116 silver badges171 bronze badges asked 15 hours ago Radim VansaRadim Vansa 5,8883 gold badges28 silver badges45 bronze badges 2
  • 2 Trailing comma has always been allowed in initializer lists. What C99 changed was just to also allow it in enum lists. – Lundin Commented 15 hours ago
  • 2 Anyway this sounds like something that should be added by a code formatter/beautifier. Not really the compiler's job. – Lundin Commented 15 hours ago
Add a comment  | 

1 Answer 1

Reset to default 1

No, compiler can not require this, since presence or absence of trailing comma are not an error and cannot really lead to an error.

But you can use code formatters for this. For example, clang-format has a special setting for it: https://clang.llvm.org/docs/ClangFormatStyleOptions.html#inserttrailingcommas And yes, you can install and use clang-format without clang.

Many text editors can run formatter automatically on save. Or you can run formatter as pre-commit hook in git.

本文标签: cRequire trailing commaStack Overflow