You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过clang-tidy为const引用及常量参数指定命名大小写?

解决clang-tidy针对常量参数(含const引用)的命名规则配置问题

需求:为示例代码中func的const int& a和const int b参数指定大写命名规则,不对int c生效。

现有尝试的问题

  • 使用ParameterCase会匹配所有参数(包括c),无法排除目标外的参数
  • 使用ConstantParameterCase仅匹配值传递的const参数b,不覆盖const引用类型的a

可行配置方案

在clang-tidy 15.0.7版本中,可通过组合ConstantParameterCase、ReferenceParameterCase,并配合ParameterIgnoredRegexp排除非目标参数,命令如下:

clang-tidy -checks='readability-identifier-naming' -config="{CheckOptions: [
  {key: readability-identifier-naming.ConstantParameterCase, value: UPPER_CASE},
  {key: readability-identifier-naming.ReferenceParameterCase, value: UPPER_CASE},
  {key: readability-identifier-naming.ParameterIgnoredRegexp, value: '^c$'}
]}" a.cpp -- -std=c++17

配置说明

  • ConstantParameterCase:对值传递的const类型参数(如const int b)应用大写命名规则
  • ReferenceParameterCase:对引用类型参数(包括const int& a这类const引用)应用大写命名规则
  • ParameterIgnoredRegexp:通过正则^c$精准匹配参数c,将其排除在命名检查之外

相关规则文档翻译(来自clang-tidy readability-identifier-naming检查)

  • ParameterCase:所有函数参数的命名风格配置,默认无强制约束
  • ConstantParameterCase:针对值传递的const参数的命名风格配置
  • ReferenceParameterCase:针对引用类型参数(含const引用)的命名风格配置
  • ParameterIgnoredRegexp:设置正则表达式,匹配到的参数将跳过命名检查

内容的提问来源于stack exchange,提问作者Luke Videckis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 01:45:20