在 if() while() for() 之后的语句周围添加 {}

标签 clang-format

我想知道是否可以设置 clang-format 以减少非 compound_statement进入 { non-compound_statement; }iteration_statement .

statement
: labeled_statement
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
;
iteration_statement
: WHILE '(' expression ')' statement
| DO statement WHILE '(' expression ')' ';'
| FOR '(' expression_statement expression_statement ')' statement
| FOR '(' expression_statement expression_statement expression ')' statement
;

示例

输入:
if (exp) foo = 1;

输出:
if (exp) { foo = 1; }

然后美化器会根据需要缩进。

最佳答案

您想要做的超出了 clang-format 试图实现的范围:

  • The only lexical elements clang-format should touch are: whitespaces, string-literals and comments. Any other changes ranging from ordering includes to removing superfluous paranthesis are not in the scope of this tool.


来源:http://clang-developers.42468.n3.nabble.com/Design-clang-format-td3980439.html

然而,clang tidy 可以,功能标志被称为 readability-braces-around-statements .

来源:http://clang.llvm.org/extra/clang-tidy/checks/readability-braces-around-statements.html

关于在 if() while() for() 之后的语句周围添加 {},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047952/

相关文章:

c++ - 特征矩阵初始化的 clang 格式

c++ - 在新行上使用 clang-format 的右括号

c++ - Clang 格式的换行符

c++ - 使 Clang-Format 忽略换行符的注释

c - 如何更改 `clang-format` 选项以防止制动支架

c++ - clang-format 堆栈所有 if 语句参数(如果它们太长)

c++ - 如何为三元运算符强制斩线?

c++ - 如何获取 clang-format 以对齐链式方法调用

c++ - clang-format noop .clang-格式文件

c++ - 通过 clang-format 实现格式化的好方法是什么?