1

我找不到用于修复以下错误的 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]$ 

我只是想知道如何让 clang-tidy 自动解决这个问题。如果没有,我应该如何使用gsl::at()来解决这个问题?clang-tidy 文档说明如下:

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

4

1 回答 1

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
于 2018-08-10T02:42:19.163 回答