【问题标题】:Remove line break after single-statement if with clang-format如果使用 clang 格式,则在单语句后删除换行符
【发布时间】:2020-03-18 13:20:17
【问题描述】:

我正在尝试使用 clang-format 将 if 语句与一条语句放在同一行。

示例输入:

if(is_finished) break;
if(foo) {
    // do something
}

clang 格式输出:

if(is_finished)
    break;
if(foo) {
    // do something
}

想要的输出:

if(is_finished) break;
if(foo) {
    // do something
}

似乎没有一个与空间相关的选项与这种风格相匹配。

当前配置:

---
Language: Cpp 
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: ForIndentation
SpaceBeforeParens: Never
BraceWrapping:
  AfterControlStatement: false

【问题讨论】:

标签: c++ clang-format


【解决方案1】:

相关的配置选项是AllowShortIfStatementsOnASingleLine

选择是:

  • Never
  • WithoutElse
  • Always

https://clang.llvm.org/docs/ClangFormatStyleOptions.html

【讨论】:

    【解决方案2】:
    AllowShortIfStatementsOnASingleLine
    

    AllowShortBlocksOnASingleLine
    

    第一个做你想做的,AllowShortBlocksOnASingleLine也会允许这样的代码

    if (expression) { Something(); }
    

    【讨论】:

      猜你喜欢
      • 2022-11-08
      • 2014-07-26
      • 2016-07-13
      • 2020-04-05
      • 2020-04-02
      • 2016-02-12
      • 2019-06-18
      • 1970-01-01
      • 2015-10-15
      相关资源
      最近更新 更多