我是第一次使用 clang-tidy。我正在使用 OpenCV 完成我的论文。我的问题是,当我将 clang-tidy 配置为“修复”它发现的问题时,它也会修复它在 OpenCV 库中发现的问题。我的问题是:如何才能使 clang-tidy 只修复它在我创建的文件中发现的问题?
我的项目中有依赖项作为源,我无法控制.我正在使用cmake的clang-tidy集成来分析我的代码,而这种依赖性正在发出很多警告.有没有办法告诉cmake不要在特定文件上运行clang-tidy?
我试图将文件添加到-line-filterclang-tidy选项中,但这不起作用:
set_target_properties(target PROPERTIES
CXX_CLANG_TIDY "${clang_tidy_loc};\
${TIDY_CONFIG} \
-line-filter=\"[\
{\"name\":\"path/to/file.cpp\"},\
{\"name\":\"path/to/file.h\"}\
]\"")
Run Code Online (Sandbox Code Playgroud)
如果解决方案可以与其他静态分析器(如cppcheck)一起使用,那将非常好.谢谢.
我有如下代码
int do_thing( int arg1, int arg2, int arg3 ) { // NOLINT : don't care that arg2 and arg3 aren't used
return arg1;
}
Run Code Online (Sandbox Code Playgroud)
NOLINT 注释通常会抑制清晰的警告。但是,如何在不更改代码的情况下“重新启用”该行的叮当声?
我正在使用 cmdline 中的 clang-tidy clang-tidy readability-implicit-bool-conversion ... <other_options>"。这个 clang-tidy 选项有子选项AllowPointerConditions,为了让 clang-tidy 允许我使用if (!p)任何警告。
https://clang.llvm.org/extra/clang-tidy/checks/readability-implicit-bool-conversion.html
如何从命令行启用此功能?谢谢
Checks: 'modernize-use-auto, modernize-use-nullptr, modernize-loop-convert'
CheckOptions:
- key: modernize-loop-convert.MinConfidence
value: safe
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的配置文件,想要添加-fix标志来应用可能的修复,但我无法弄清楚它的确切位置。是否可以向文件添加-fix或-fix-errors标记.clang-tidy?
clang-tidy [bugprone-in Correct-roundings]检查的文档说:
数字 0.499999975(小于 0.5 的最小可表示浮点数)四舍五入为 1.0
据我所知,下面的最小浮点数0.5是0.4999999702,而不是0.499999975。但尽管如此,这两个数字都给出了0朴素舍入计算中的值:
#include <iostream>
int main() {
const float v1 = 0.499999975;
const float v2 = 0.4999999702;
std::cout << (int)(v1+0.5) << "\n"
<< (int)(v2+0.5) << "\n";
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我的理解是这ConcreteType&&是一个右值并且TemplateType&&是“完美转发”。
我正在尝试使用完美转发,但 clang-tidy 将其解释为右值引用。
clang 和 gcc 不会抱怨,但在进行任何进一步分析之前,clang-tidy 将其标记为解析错误,所以我不确定这是代码问题还是 clang-tidy 问题。
我怀疑这与我在构造函数中使用它有关,但我不确定。
最低代码:
#include <memory>
template <typename T>
class my_wrapper {
T val;
public:
my_wrapper(T&& val_)
: val{std::forward<T>(val_)} {}
};
struct my_struct {
int x;
};
auto xyz() {
my_struct ms{3};
return my_wrapper<my_struct>(ms);
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
Error while processing /path/foo.cpp.
/path/foo.cpp:20:25: error: no matching constructor for initialization of 'my_wrapper<my_struct>' [clang-diagnostic-error]
my_wrapper<my_struct> wrap(ms);
^
/path/foo.cpp:5:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from …Run Code Online (Sandbox Code Playgroud) 我正在用 C 编写一个处理字符串的函数,它是递归的。基本上它所做的是在某些字符和'\0'. 如果在找到之前'\0',它命中了特定的字符,它将递归地调用自己。
在 CLion 中编写它时,我看到了 Clang-Tidy 的警告,这是我以前从未见过的。它说
Clang-Tidy:函数“function”在递归调用链中
我想知道它是 CLion 20.02 的新功能吗(我最近更新了它)?此外,我该如何解决?
这是我的代码。
char *function(char *pos, <some arguments>) {
char *temp = pos + 1;
while (1) {
if (*temp == '\0') {
return temp;
} else if (*temp == '<something>') {
*temp = '\0';
if (*(temp + 1) == '\0') {
return function(temp + 1, <some arguments>);
} else if (*(temp + 1) == '<something>') {
if (*(temp + 2) == '\0') { …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Clang-Tidy 静态分析工具集成到我的构建系统中。我的设置遇到一些问题。
步骤cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..不生成compile_commands.json文件。因此,clang-tidy 给出了以下错误。
>cmake --build .结果
Could not auto-detect compilation database from directory "C:/dev/my-project/build/compile_commands.json"
No compilation database found in C:\dev\my-project\build\compile_commands.json or any parent directory
fixed-compilation-database: Error while opening fixed database: no such file or directory
json-compilation-database: Error while opening JSON database: no such file or directory
Running without flags.
Error while processing C:\dev\my-project\src\.
CUSTOMBUILD : error : unable to handle compilation, expected exactly one compiler job in '' [clang-diagnostic-error] [C:\dev\my-project\build\analyze_clang_tidy.vcxproj]
Suppressed 1 warnings …Run Code Online (Sandbox Code Playgroud) 当参数类型位于命名空间中时,可以省略sstd::的命名空间(通常是这种情况)。<algorithm>是否有任何警告或整齐的规则可以发现此类遗漏?
#include <vector>
#include <algorithm>
std::vector<int> v;
for_each(v.begin(), v.end(), [](auto){});
return 0;
Run Code Online (Sandbox Code Playgroud)
上面的示例使用最新的 clang 和 -Wall、-Wextra 和 -Wpedantic 编译,不会发出任何诊断信息: