Clang-tidy 是一个比较实用的代码检查程序,它可以检测出很多笔误等低级错误,也会提出一些已经得到广泛验证的 best practice,不过提供的检查数量众多,有些过于严苛,还有一些互相矛盾(比如 fuchsia-trailing-return
建议你永远不要写 尾随返回类型,但 modernize-use-trailing-return-type
建议你总是这样写),所以我们必须有选择地打开一部分真正有必要的检查。
我过了一遍目前(2022-01-19)提供的 所有检查项目,然后根据 CLion 默认设置的建议,以及很少量的个人经验,列出了比较推荐打开的检查(可以直接粘贴到 CLion 设置中),我筛选的原则是:
- 平台相关的(比如安卓)检查项目不列入;
- 特定第三方库相关的检查项目不列入;
- 矛盾的两个项目,都不列入;
- 与某些 无害的 编程习惯有关,且对可读性影响也不大的,由于因人而异,不列入(比如:应该写
if {... return;} else {... return;}
还是if {... return;} ... return;
?); - 少数会引起警告,但太过普遍的做法,不列入(比如:将数组名退化为首地址指针作实参,会引起警告;字符数组退化为字符常量指针也不例外);
- 如果一个检查项目会干扰 常见的、故意设计的行为,且这样写的后果能够在字面上立即预测出时,不列入该项目(比如:某个检查会在随机数发生器使用确定数字作为种子时发出警告,提示“这会产生可预期的随机序列”,而这显然正是我们的意图);
- 除以上情况之外,都列入。
-*
bugprone-assert-side-effect
bugprone-bad-signal-to-kill-thread
bugprone-bool-pointer-implicit-conversion
bugprone-branch-clone
bugprone-copy-constructor-init
bugprone-dangling-handle
bugprone-dynamic-static-initializers
bugprone-exception-escape
bugprone-fold-init-type
bugprone-forward-declaration-namespace
bugprone-forwarding-reference-overload
bugprone-implicit-widening-of-multiplication-result
bugprone-inaccurate-erase
bugprone-incorrect-roundings
bugprone-integer-division
bugprone-lambda-function-name
bugprone-macro-repeated-side-effects
bugprone-misplaced-operator-in-strlen-in-alloc
bugprone-misplaced-pointer-arithmetic-in-alloc
bugprone-misplaced-widening-cast
bugprone-move-forwarding-reference
bugprone-multiple-statement-macro
bugprone-no-escape
bugprone-not-null-terminated-result
bugprone-parent-virtual-call
bugprone-posix-return
bugprone-signal-handler
bugprone-signed-char-misuse
bugprone-sizeof-container
bugprone-sizeof-expression
bugprone-spuriously-wake-up-functions
bugprone-string-constructor
bugprone-string-integer-assignment
bugprone-string-literal-with-embedded-nul
bugprone-stringview-nullptr
bugprone-suspicious-enum-usage
bugprone-suspicious-include
bugprone-suspicious-memory-comparison
bugprone-suspicious-memset-usage
bugprone-suspicious-missing-comma
bugprone-suspicious-semicolon
bugprone-suspicious-string-compare
bugprone-swapped-arguments
bugprone-terminating-continue
bugprone-throw-keyword-missing
bugprone-too-small-loop-variable
bugprone-undefined-memory-manipulation
bugprone-undelegated-constructor
bugprone-unhandled-exception-at-new
bugprone-unhandled-self-assignment
bugprone-unused-raii
bugprone-unused-return-value
bugprone-use-after-move
bugprone-virtual-near-miss
cert-dcl21-cpp
cert-dcl50-cpp
cert-dcl58-cpp
cert-err33-c
cert-err34-c
cert-err52-cpp
cert-err58-cpp
cert-err60-cpp
cert-flp30-c
cert-mem57-cpp
cert-msc50-cpp
cert-str34-c
cert-oop57-cpp
cert-oop58-cpp
concurrency-*
cppcoreguidelines-init-variables
cppcoreguidelines-macro-usage
cppcoreguidelines-narrowing-conversions
cppcoreguidelines-no-malloc
cppcoreguidelines-prefer-member-initializer
cppcoreguidelines-pro-bounds-pointer-arithmetic
cppcoreguidelines-pro-type-member-init
cppcoreguidelines-pro-type-static-cast-downcast
cppcoreguidelines-slicing
cppcoreguidelines-special-member-functions
cppcoreguidelines-virtual-class-destructor
fuchsia-multiple-inheritance
google-build-explicit-make-pair
google-default-arguments
google-explicit-constructor
google-readability-avoid-underscore-in-googletest-name
google-runtime-operator
google-upgrade-googletest-case、
hicpp-exception-baseclass
hicpp-multiway-paths-covered
hicpp-no-assembler
misc-definitions-in-headers
misc-misleading-bidirectional
misc-misplaced-const
misc-new-delete-overloads
misc-non-copyable-objects
misc-static-assert
misc-throw-by-value-catch-by-reference
misc-unconventional-assign-operator
misc-uniqueptr-reset-release
modernize-avoid-bind
modernize-concat-nested-namespaces
modernize-deprecated-headers
modernize-deprecated-ios-base-aliases
modernize-loop-convert
modernize-make-shared
modernize-make-unique
modernize-pass-by-value
modernize-raw-string-literal
modernize-redundant-void-arg
modernize-replace-auto-ptr
modernize-replace-disallow-copy-and-assign-macro
modernize-replace-random-shuffle
modernize-return-braced-init-list
modernize-shrink-to-fit
modernize-unary-static-assert
modernize-use-auto
modernize-use-bool-literals
modernize-use-default-member-init
modernize-use-emplace
modernize-use-equals-default
modernize-use-equals-delete
modernize-use-nodiscard
modernize-use-noexcept
modernize-use-nullptr
modernize-use-override
modernize-use-transparent-functors
modernize-use-uncaught-exceptions
mpi-*
openmp-use-default-none
performance-faster-string-find
performance-for-range-copy
performance-implicit-conversion-in-loop
performance-inefficient-algorithm
performance-inefficient-string-concatenation
performance-inefficient-vector-operation
performance-move-const-arg
performance-move-constructor-init
performance-no-automatic-move
performance-no-int-to-ptr
performance-noexcept-move-constructor
performance-trivially-destructible
performance-type-promotion-in-math-fn
performance-unnecessary-copy-initialization
performance-unnecessary-value-param
portability-simd-intrinsics
readability-avoid-const-params-in-decls
readability-braces-around-statements
readability-const-return-type
readability-container-data-pointer
readability-container-size-empty
readability-convert-member-functions-to-static
readability-delete-null-pointer
readability-deleted-default
readability-inconsistent-declaration-parameter-name
readability-make-member-function-const
readability-misleading-indentation
readability-misplaced-array-index
readability-non-const-parameter
readability-qualified-auto
readability-redundant-access-specifiers
readability-redundant-control-flow
readability-redundant-declaration
readability-redundant-function-ptr-dereference
readability-redundant-member-init
readability-redundant-preprocessor
readability-redundant-smartptr-get
readability-redundant-string-cstr
readability-redundant-string-init
readability-simplify-boolean-expr
readability-simplify-subscript-expr
readability-static-accessed-through-instance
readability-static-definition-in-anonymous-namespace
readability-string-compare
readability-uniqueptr-delete-release
readability-use-anyofallof
列表第一行
-*
是“未提及的检查默认保持禁用”的意思。
还有 3 条与 CLion 原生提供的检查重合了,不需要打开的项目(其它 IDE 如果没提供类似的检查,则建议打开):