我找不到用于解决以下错误的clang-tidy命令行的示例代码:
[archlinux@thinkpad fizzbuzz]$ clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index
5 warnings generated.
fizzbuzz2.cpp:21:18: warning: do not use array subscript when the index is not an integer constant expression; use gsl::at() instead [cppcoreguidelines-pro-bounds-constant-array-index]
std::cout << arr[index] << std::endl;
^
Suppressed 4 warnings (4 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
[archlinux@thinkpad fizzbuzz]$
我只想了解如何获得自动更新的提示。如果没有,我应该如何使用gsl::at()
来解决此问题?整洁的文档说以下内容:
cppcoreguidelines-pro-bounds-constant-array-index
This check flags all array subscript expressions on static arrays and std::arrays that either do not have a constant integer expression index or are out of bounds (for std::array). For out-of-bounds checking of static arrays, see the -Warray-bounds Clang diagnostic.
This rule is part of the “Bounds safety” profile of the C++ Core Guidelines, see https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Pro-bounds-arrayindex.
Options
GslHeader
The check can generate fixes after this option has been set to the name of the include file that contains gsl::at(), e.g. “gsl/gsl.h”.
IncludeStyle
A string specifying which include-style is used, llvm or google. Default is llvm.
我在gsl::at()
中找到了<gsl/gsl_util>
子例程。如何告诉clang-tidy使用它来修正警告?
编辑:我查看了配置字符串并找到了解决方案:
正在运行:
clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index -dump-config
我对解决方案有一些提示
clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index -config="{CheckOptions: [{key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader, value: gsl/gsl_util}]}" -fix
答案 0 :(得分:0)
编辑:我查看了配置字符串并找到了解决方案:
正在运行:
clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index -dump-config
我对解决方案有一些提示
clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index -config="{CheckOptions: [{key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader, value: gsl/gsl_util}]}" -fix