Skip to content

Conversation

@kurisaW
Copy link
Member

@kurisaW kurisaW commented Sep 24, 2025

拉取/合并请求描述:(PR description)

[

优化clang-format规则,使其更加满足RT-Thread编码规范

主要修改内容:

  1. 对齐规则调整:禁用连续赋值和声明对齐(AlignConsecutiveAssignmentsAlignConsecutiveDeclarations 设为 false),数组结构对齐改为 None
  2. 注释与换行:尾随注释对齐改为保留原样(Leave),case 标签后换行(AfterCaseLabel: true)。
  3. 函数与命名空间:允许短函数内联显示(Inline),命名空间内容全部缩进(All)。

]

@github-actions
Copy link

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below.


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:format
  • 设置PR number为 \ Set the PR number to:10736
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 format 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the format branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@Rbb666
Copy link
Member

Rbb666 commented Sep 25, 2025

这个 PR 修改了 .clang-format 文件,调整了代码格式化的具体规则。下面我用简单代码片段举例说明几个主要变化:


1. AlignArrayOfStructures: Right → None

原来:

struct {
    int   a;
    float b;
} arr[2];

数组元素的字段会“右对齐”。

现在:

struct {
    int a;
    float b;
} arr[2];

字段不再对齐,直接按原始书写方式排列。


2. AlignConsecutiveAssignments/Declarations: Enabled: true → false

原来:

int   a    = 1;
float b    = 2.0;
char  c    = 'x';

连续赋值、声明会被对齐等号或类型。

现在:

int a = 1;
float b = 2.0;
char c = 'x';

不再自动对齐,保持原始间距。


3. AlignTrailingComments: Kind: Always → Leave

原来:

int a = 1;    // comment
int b = 2;    // another comment

所有同行注释会被对齐。

现在:

int a = 1; // comment
int b = 2; // another comment

注释不会强制对齐,留在原位。


4. AllowShortFunctionsOnASingleLine: None → Inline

原来:

int foo() 
{
    return 1;
}

即使很短,也不会一行写。

现在:

int foo() { return 1; }

短函数允许写在一行,更简洁。


5. BraceWrapping: AfterCaseLabel: false → true

原来:

switch (x) {
    case 1:
        doSomething();
}

case 后不换行加 {

现在:

switch (x) {
    case 1:
    {
        doSomething();
    }
}

case 后会自动换行加 {},结构更规范。


6. NamespaceIndentation: None → All

原来:

namespace foo {
int a;
}

namespace 内部没有缩进。

现在:

namespace foo {
    int a;
}

namespace 内部内容会缩进,提高可读性。


这些改动主要影响代码对齐、注释、缩进和短结构的写法,让格式更紧凑或更规范,具体效果可在实际格式化后直观体现。

@Rbb666
Copy link
Member

Rbb666 commented Sep 25, 2025

@kurisaW 这个确认下修改后是否会这样,不建议格式化成同行显示,会降低可阅读性

image

@kurisaW
Copy link
Member Author

kurisaW commented Sep 25, 2025

补充格式化说明(以下测试情况都是根据最新修改format脚本进行):


1. AlignArrayOfStructures: Right → None

格式化前:

struct {
    int a;
    float b;
} arr[2];

格式化后:

struct
{
    int a;
    float b;
} arr[2];

2. AlignConsecutiveAssignments/Declarations: Enabled: true → false

格式化前:

int   a    = 1;
float b    = 2.0;
char  c    = 'x';

现在:

int a = 1;
float b = 2.0;
char c = 'x';

ps:这里不建议根据运算符对齐,否则如果遇到命名很长的声明,其他底下的名字会看起来很突兀


3. AlignTrailingComments: Kind: Always → Leave

格式化前:

int a = 1;    // comment
int b = 2;    // another comment

格式化后:

int a = 1;    // comment
int b = 2;    // another comment

注释不会强制对齐,留在原位。


4. AllowShortFunctionsOnASingleLine: None → Inline

格式化前:

int foo() { return 1; }

即使很短,也不会一行写。

格式化后:

int foo()
{
    return 1;
}

5. BraceWrapping: AfterCaseLabel: false → true

格式化前:

switch (x) {
    case 1:
        doSomething();
}

格式化后:

switch (x)
{
case 1:
    doSomething();
}

6. NamespaceIndentation: None → All

格式化前:

namespace foo {
int a;
}

namespace 内部没有缩进。

现在:

namespace foo
{
    int a;
}

@Rbb666 Rbb666 merged commit 414df9d into RT-Thread:master Sep 25, 2025
68 checks passed
@kurisaW kurisaW deleted the format branch September 25, 2025 06:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants