在启用 c++20 的情况下编译时,clang-tidy 发出警告

wyc*_*ter 5 c++ c++20 clang-tidy

我最近更新了我的项目及其 CI 管道以使用 C++20 功能。当我将编译器设置更改为使用 C++20 时,clang-tidy 开始为我检查的所有文件发出以下警告(我将它们视为错误):

error: invalid case style for template parameter 'expr-type' [readability-identifier-naming,-warnings-as-errors]
Run Code Online (Sandbox Code Playgroud)

我可以使用 CMake 和一个 clang-tidy 配置文件将其缩小到以下最小示例:

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(clang_tidy_warning LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(clang_tidy_warning main.cpp)
Run Code Online (Sandbox Code Playgroud)

.clang-tidy

Checks: >
  readability-*,

WarningsAsErrors: '*'

CheckOptions:
  - {key: readability-identifier-naming.TemplateParameterCase,           value: lower_case}
  - {key: readability-identifier-naming.TypeTemplateParameterCase,       value: CamelCase }
  - {key: readability-identifier-naming.TemplateParameterPrefix,         value: t_ }
  - {key: readability-identifier-naming.TypeTemplateParameterPrefix,     value: T_ }
Run Code Online (Sandbox Code Playgroud)

主程序

error: invalid case style for template parameter 'expr-type' [readability-identifier-naming,-warnings-as-errors]
Run Code Online (Sandbox Code Playgroud)

您应该能够在 Ubuntu 20.04 中使用以下命令在根目录中重现它:

mkdir build
cd build
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=DEBUG ..
run-clang-tidy-12  -quiet
Run Code Online (Sandbox Code Playgroud)

如果您执行以下操作之一,错误/警告就会消失:

  • 从配置文件中删除指定的检查选项
  • 将 c++ 标准设置为 17
  • 删除#include主文件中的

iostream与另一个标准库头文件交换没有帮助,但我还没有对它们进行全部测试。还值得一提的是,所有其他可能的 clang-tidy 配置选项都不会导致此错误。我几乎所有这些都在原始配置文件中,并删除了不会导致错误的那些。似乎只有TemplateParameterTypeTemplateParameter子选项受此影响。我已经测试了 clang-tidy 版本 9、10 和 12。都发出相同的警告。我还测试了将不同的编译器(clang 12 和 gcc10)传递给 CMake。没有改变任何东西(不知道 clang-tidy 是否甚至受到编译器选择的影响,我猜不是)。我使用的是 Ubuntu 20.04(最新版本),您还可以在 GitHub 操作运行程序中重现该错误。

所以我的问题是。这是一个(已知的)错误还是在这个叮叮当当的设置中与 C++ 20 不兼容?如果它是已知的,您能否解释一下它发生的原因以及如何解决它?

编辑

正如 Lawrence Benson 在评论中提到的,这也可能与操作系统有关。他无法在 Mac 上重现,但在 Ubuntu 上。

相关错误报告

这是 0x5453 发现的相关错误报告的链接:单击我

wyc*_*ter 4

这个bug在14版本中仍然没有修复。

然而,正如“Schrodinger ZHU”已经在问题评论中暗示的那样,并且正如用户“saitou1024”在Github 上的错误报告中所示,可以使用readability-identifier-naming.TypeTemplateParameterIgnoredRegexp解决这个问题。

将以下行添加CheckOptions到您的 clang-tidy 配置文件中:

CheckOptions:  
  - {key: readability-identifier-naming.TypeTemplateParameterIgnoredRegexp, value: expr-type}
Run Code Online (Sandbox Code Playgroud)

对我来说,这解决了问题,我现在可以readability-identifier-naming再次将警告视为错误,以在 CI 管道中强制执行命名约定。

附加说明:

由于上面的行仅影响选项,并且指定选项TypeTemplateParameter时也会出现错误,因此请注意还有一个选项。因此,如果此解决方法无法解决您的问题,请尝试使用添加同一行。TemplateParameterTemplateParameterIgnoredRegexpTemplateParameterIgnoredRegexp