我不明白llvmlibc-restrict-system-libc-headersClang Tidy 中的检查(链接)。
我在 C++ 代码中包含 C 库,如下所示:
#include <cstddef>
Run Code Online (Sandbox Code Playgroud)
我应该更改什么来修复此 Clang Tidy 检查?它应该被修复吗?
请帮助我理解如何从 clang-tidy 配置文件中注释掉一行。
我正在研究 clang-tidy 到处标记的代码
Loop variable is copied but only used as const reference; consider making it a const reference
Run Code Online (Sandbox Code Playgroud)
当前代码:
for (auto foo : collection) {
...
}
Run Code Online (Sandbox Code Playgroud)
clang-tidy 建议我使用什么:
for (const auto &foo : collection) {
...
}
Run Code Online (Sandbox Code Playgroud)
我当然可以看到 const 引用比实际复制值更有效,但我想知道编译器是否会以这种方式优化它?我不想白白地深入研究并更改数百个循环。
我正在使用 clang-tidy 来检查我的代码库,但整个过程非常慢。有没有办法完全忽略头文件而不仅仅是抑制警告?正如您在这个示例中看到的,大量警告来自我的项目依赖项:
Suppressed 72958 warnings (72958 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.
Run Code Online (Sandbox Code Playgroud) 我有一个通过cmake使用clang-tidy的构建:
set_target_properties(project
PROPERTIES
...
CXX_CLANG_TIDY
"/usr/bin/clang-tidy"
"-checks=modernize-*,readability-*,performance-*"
"-fix"
)
Run Code Online (Sandbox Code Playgroud)
构建它时,我在Qt库中可能会出现内存泄漏:
/opt/Qt5.7.0/5.7/gcc_64/include/QtCore/qobject.h:242:16: warning: Potential memory leak [clang-analyzer-cplusplus.NewDeleteLeaks]
return connectImpl(sender, reinterpret_cast<void **>(&signal),
^
.../a.cpp:27:5: note: Taking false branch
if (not inputQFile.makeAbsolute()) {
^
.../a.cpp:33:5: note: Calling 'QObject::connect'
connect(this, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
^
/opt/Qt5.7.0/5.7/gcc_64/include/QtCore/qobject.h:238:13: note: Left side of '||' is false
if (type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection)
^
/opt/Qt5.7.0/5.7/gcc_64/include/QtCore/qobject.h:238:9: note: Taking false branch
if (type == Qt::QueuedConnection || type == Qt::BlockingQueuedConnection)
^
/opt/Qt5.7.0/5.7/gcc_64/include/QtCore/qobject.h:242:16: note: Potential memory leak
return connectImpl(sender, reinterpret_cast<void **>(&signal), …Run Code Online (Sandbox Code Playgroud) 提供以下代码,在全局范围内,clang-tidy不提供警告:
auto test = []{};
Run Code Online (Sandbox Code Playgroud)
但是,在执行以下操作时,它会:
#include <tuple>
auto test = []{
std::tuple t{1, 2, 3};
};
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)<source>:3:6: warning: initialization of 'test' with static storage duration may throw an exception that cannot be caught [cert-err58-cpp] auto test = []{ ^ /opt/compiler-explorer/gcc-8.2.0/lib/gcc/x86_64-linux-gnu/8.2.0/../../../../include/c++/8.2.0/tuple:646:19: note: possibly throwing constructor declared here constexpr tuple(_UElements&&... __elements) ^
将lambda标记为noexcept没有用.
但是,我不明白为什么会出现这个问题.异常只能在调用 lambda 时理论上发生,不是吗?
以下代码不会导致出现警告:
auto test = [] {
throw 0;
};
Run Code Online (Sandbox Code Playgroud)
铿锵有误,还是我错过了什么?
在以下函数中,我进行初始化args,在调用中使用它们va_start,然后调用va_end。
该代码对我来说似乎正确,但是clang-tidy发出警告:
tmp2.c:7:11: error: Function 'vsnprintf' is called with an uninitialized va_list argument [clang-analyzer-valist.Uninitialized,-warnings-as-errors]
len = vsnprintf((void*)0, 0, format, args);
#include<stdarg.h>
#include<stdio.h>
int f(char *format, ...) {
int len;
va_list args;
va_start(args, format);
len = vsnprintf((void*)0, 0, format, args);
va_end(args);
return len;
}
Run Code Online (Sandbox Code Playgroud)
更奇怪的是,这只会在我一次清理多个文件时发生,因此clang-tidy tmp2.c不会发出警告,但是clang-tidy tmp2.c tmp2.c会发出警告!
这是我的代码还是clang-tidy的问题?我使用的是LLVM 7.0.0版,但8.0.0也会出现该警告。
我是 clang-tidy 的新手,以下是练习,因此我可以转向更复杂的匹配器和工具。
可以说我们有
typedef int my_type;
void foo()
{
int x = 0;//this should be identified as need to be fixed
my_type z = 0;
if( x == z){
//match this case
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标是识别与“my_type”进行比较的变量,以便通过将它们的类型更改为 my_type 来修复它们的声明。
现在我正在尝试执行以下操作
auto my_type_decl = varDecl(hasType(asString("my_type")));
auto my_type_decl_exp= declRefExpr(to(my_type_decl));
auto binop = binaryOperator(has(implicitCastExpr(has(my_type_decl_exp))));
auto other_decl_exp = declRefExpr(hasAncestor(binop), unless(to(my_type_decl)));
//get ancestor functionDecl
//get descendant varDecls that match the other_decl_exp
Run Code Online (Sandbox Code Playgroud)
这里的问题是我无视上下文。处理这样的事情的正确方法是什么?
在下面的 C 程序中,函数f、g和h本质上是相同的,但是 clang-tidy 说参数p可以是指向常量的指针 in gand h(但不是 in f)。我的理解是,p它们中的任何一个都不能是指向常量的指针。那是假阳性吗?
struct S { int *p; };
extern void x(struct S *s);
void f(int *p)
{
struct S s;
s.p = p;
x(&s);
}
void g(int *p)
{
struct S s = { .p = p };
x(&s);
}
void h(int *p)
{
struct S s = { p };
x(&s);
}
int main()
{
int a = …Run Code Online (Sandbox Code Playgroud) 我有一个自定义的前向迭代器类型。它声明(除其他外):
Iter& operator++();
Iter operator++(int);
Run Code Online (Sandbox Code Playgroud)
clang-tidy 抱怨 cert-dcl21-cpp(Clang-Tidy:重载的“operator++”返回一个非常量对象而不是一个常量对象类型)
现在,虽然我很天真,但我将声明更改为
Iter& operator++();
Iter const operator++(int);
Run Code Online (Sandbox Code Playgroud)
好吧,它现在抱怨 readability-const-return-type(返回类型 'const IndexAtomListPtr::Iter' 在顶层是 'const'-qualified,这可能会降低代码可读性而不提高 const 正确性)
我怎样才能满足clang-tidy?
相关问题的答案:重载的“operator++”返回一个非常量,并且clang-tidy抱怨是不够的,因为这正是我尝试过的,但得到了可读性-const-return-type抱怨