【问题标题】:How to remove empty expressions while formatting with clang-format?如何在使用 clang-format 格式化时删除空表达式?
【发布时间】:2022-01-23 22:26:18
【问题描述】:

有没有办法使用 clang-format 删除如下的空表达式(多余的分号)?

int main() {
  return 0;
}; <- redundant ;

由于以下其他有效情况,幼稚的搜索替换将不起作用:

struct A {
    int a;
}; <- required

搜索empty/expression/semi/colon 没有给出任何与clang-format docs相关的信息。

【问题讨论】:

  • clang 格式不负责修复/改进代码。它仅更改格式(空格、制表符、行尾、换行),因此无法添加/删除非白色字符。
  • 你可能想要 clang-tidy 而不是 clang-format 。

标签: c++ clang-format


【解决方案1】:

clang-tidy 上有一个“Bugprone suspicious semicolon”,但它不接受您提供的案例。

clang-tidy 会在正确的情况下为您修复代码。示例:

# cat test.cpp
int main()
{
};
$ clang-tidy --fix  -checks=modernize* test.cpp --
1 warning generated.
test.cpp:1:5: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
int main()
~~~ ^
auto       -> int
test.cpp:1:1: note: FIX-IT applied suggested code changes
int main()
^
test.cpp:1:11: note: FIX-IT applied suggested code changes
int main()
          ^
clang-tidy applied 2 of 2 suggested fixes.

然后代码就地固定

$ cat test.cpp
auto main() -> int
{
};

一种解决方案是通过修改 clang 工具来改进这种情况

Extending clang tooling

【讨论】:

    猜你喜欢
    • 2017-10-30
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多