如何防止 clang-format 在新行中添加单个分号?

发布于 2025-01-20 17:14:38 字数 293 浏览 4 评论 0原文

我在C ++中有这条代码行,

while (fread(pixel_array++, sizeof(byte), 3, fp));

新系列中

while (fread(pixel_array++, sizeof(byte), 3, fp))
    ;

但是当我使用Clang-Format时,它会将半隆分开,并将其添加到我不喜欢这种样式的

,我只想保留原始的样式。我应该如何修改我的clang-Format配置?谢谢。

I have this line of code in C++

while (fread(pixel_array++, sizeof(byte), 3, fp));

but when I use clang-format, it splits the semicolon and add it into a new line

while (fread(pixel_array++, sizeof(byte), 3, fp))
    ;

I don't like this kind of style, and I just prefer to keep the original one.

How should I modify my clang-format configuration? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕嗳→ 2025-01-27 17:14:38

clang-format 5.0 目前无法识别这种类型的循环。不幸的是,从 clang-format 版本 5 开始,您将无法获得满足您需要的设置。

查找 Clang 格式样式选项,最接近的我发现是 AllowShortLoopsOnASingleLine: true,但该设置无法将循环条件识别为循环体。

只要 clang-format 无法识别这些类型的循环,我要做的就是使用 // clang-format off 标记您的代码,然后使用 // clang-format位于代码块周围的 上。

clang-format 5.0 currently doesn't recognize that type of loop. Unfortunately as of clang-format version 5, you won't get a setting that does what you need.

Looking up the Clang Format Style Options, the closest that I've found is AllowShortLoopsOnASingleLine: true, but that setting doesn't recognize the loop condition as being the body of the loop.

As long as clang-format doesn't recognize those kinds of loops, what I'd do is to mark it your code with // clang-format off and then // clang-format on around your block of code.

心意如水 2025-01-27 17:14:38

显然这是不可能的,但解决方法可能是用空块替换分号。如果同时设置了AllowShortLoopsOnASingleLineAllowShortBlocksOnASingleLine,那么它将被格式化为

while (fread(pixel_array++, sizeof(byte), 3, fp)) {}

Apparently this is not possible, but a workaround could be to replace the semicolon with an empty block. If both AllowShortLoopsOnASingleLine and AllowShortBlocksOnASingleLine are set, than it will be formatted as

while (fread(pixel_array++, sizeof(byte), 3, fp)) {}
如果没有你 2025-01-27 17:14:38

当循环没有道理时,架子不会返回布尔和空的。因此,最好将代码重写为

for(;;)
{
    auto const read_bytes_count{fread(pixel_array, sizeof(byte), 3, fp)};
    if((sizeof(byte) * 3) != read_bytes_count)
    {
        // probably deal with error handling...
        break;
    }
    ++pixel_array;
}

fread does not return bool and empty while loop makes no sense. So it would be better to rewrite your code as

for(;;)
{
    auto const read_bytes_count{fread(pixel_array, sizeof(byte), 3, fp)};
    if((sizeof(byte) * 3) != read_bytes_count)
    {
        // probably deal with error handling...
        break;
    }
    ++pixel_array;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文