标签: clang-tidy

Clang-Tidy 不明确:operator++(int) 应该返回什么?

因此,我一直在致力于Vector一项作业的实现,并在实现迭代器时注意到一些奇怪的 Clang-Tidy 建议。

根据要求的规范,迭代器必须具有iterator& operator++()和重载。iterator operator++(int)现在,当我尝试定义iterator operator++(int),它本质上是复制迭代器,递增指针,然后返回该副本,Clang-Tidy 抱怨道:

重载的“operator++”返回一个非常量对象而不是常量对象类型

(显然)应该通过将返回类型更改为 来解决这个问题const iterator,否则这种技术非常罕见。不过,相应地更改签名会让 Clang-Tidy 再次抱怨:

返回类型“const Vector::iterator”(又名“const Vector::Iterator”)在顶层具有“const”限定,这可能会降低代码可读性,而不会提高 const 正确性

CLion 2020.3.2 中的默认修复操作实际上删除了const,这会带回原始的 Clang-Tidy 警告。

我想知道什么是“正确”的实施方式operator++(int)。在这种情况下我应该忽略 Clang-Tidy 吗?如果是,我应该选择哪个签名?

谢谢!

编辑1

以下帖子没有回答我的问题:Other StackOverflow Question。事实上,在我决定问自己的问题之前我就看到了这篇文章,因为它没有为我解答。问题是在链接的帖子中建议了两个选项:

  1. 按照 clang-tidy 所说的去做,但可能会失去移动语义的好处。
  2. 相反,左值引用限定重载,以模仿小整数。

(1) 的意思正是我所做的,即将 a 放在返回类型const前面iterator。正如我在帖子中所述,这会触发第二个 Clang-Tidy 警告。

(2) 不消除 Clang-Tidy 警告。我也不确定我是否可以在作业的限制下使用它。

另外,这是一个最小的可重现示例:

class Iterator {
    ptr_type ptr; // whatever your pointer type is …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading clang-tidy

5
推荐指数
1
解决办法
950
查看次数

std::function 的“潜在内存泄漏”

考虑这个例子:

#include <vector>
#include <string>
#include <functional>
#include <iostream>

using closure_type = std::function<void(void)>;
using closure_vec = std::vector<closure_type>;

class callbacks {
    static closure_type common(std::string name, uint32_t number) {
        return [number, name]() { std::cout << name << number << std::endl; };
    }
public:
    static closure_type foo(uint32_t number) { return common("foo ", number); }
    static closure_type print(std::string msg) {
        return [msg]() { std::cout << "print " << msg << std::endl; };
    }
};

template <typename... calls_t> closure_vec wrap(uint32_t number, calls_t &&... calls) { …
Run Code Online (Sandbox Code Playgroud)

c++ clang-tidy

5
推荐指数
1
解决办法
746
查看次数

clang-tidy 会使扫描构建变得多余吗?

我有一个项目当前同时使用 scan-build 和 clang-tidy (通过 CMake 启用)。

如果我clang-analyzer-*在一组 clang-tidy 检查中启用,则使用 scan-build 是否多余?

作为参考,这里有一个关于 clang-tidy 和 clang-check 的用法的类似问题。

clang clang-static-analyzer scan-build clang-tidy

5
推荐指数
1
解决办法
1990
查看次数

“Clang-Tidy:使用 std::forward 和 const char* 时,不要将数组隐式衰减为指针”

Clang-Tidy: Do not implicitly decay an array into a pointer 我不明白为什么 Clang-Tidy在以下示例中产生错误:

struct Foo {
  explicit Foo(std::string _identifier) : identifier(std::move(_identifier)) {}

  std::string identifier;
};

struct Bar {
  template<typename... Ts>
  explicit Bar(Ts &&...args) : foo(std::forward<Ts>(args)...) {} // <-- Error

  Foo foo;
};

Bar bar("hello world");

Run Code Online (Sandbox Code Playgroud)

从错误中我了解到,"hello world"作为类型(或类似)的数组const char[11]会在 期间衰减到const char*某个位置std::forward。但为什么以及如何修复这个错误呢?std::make_shared<Foo>("hello world")与 的用法非常相似std::forward并且确实有效。

c++ c++11 clang-tidy

5
推荐指数
1
解决办法
1637
查看次数

clang-tidy-10 和compile_commands.json 不支持相对路径

有很多关于此的帖子 - 但我似乎找不到明确的答案。有些人说它有效,有些人则不然,还有人说它的工具/IDE 才是问题所在。

所以我尽可能地做了一个小例子。这是我有的两个文件:

编译命令.json

[
    {
        "arguments": [ "gcc", "test.cpp" ],
        "directory": "/home/user/development/sandbox/comp_commands",
        "file": "test.cpp"
    }
]
Run Code Online (Sandbox Code Playgroud)

测试.cpp

bool is_it()
{
    // no return value - give clang warning
}
Run Code Online (Sandbox Code Playgroud)

clang-tidy test.cpp现在,如果我在与我得到的文件相同的目录中运行该命令:

[user] comp_commands$ clang-tidy test.cpp 
1 warning generated.
test.cpp:4:1: warning: non-void function does not return a value [clang-diagnostic-return-type]
}
Run Code Online (Sandbox Code Playgroud)

...正如预期的那样。但是,如果我将compile_commands.json 目录中的行更改为:

[user] comp_commands$ clang-tidy test.cpp 
1 warning generated.
test.cpp:4:1: warning: non-void function does not return a value [clang-diagnostic-return-type]
}
Run Code Online (Sandbox Code Playgroud)

然后我得到输出:

[user] comp_commands$ clang-tidy test.cpp 
Skipping /home/user/development/sandbox/comp_commands/test.cpp. …
Run Code Online (Sandbox Code Playgroud)

c++ clang-tidy

5
推荐指数
1
解决办法
1649
查看次数

如何使用 clang-format 配置命名约定?

我的团队有兴趣添加一个工具来强制执行 C++ 的样式指南。我们研究过的两个最好的选择是clang-tidyclang-format

clang-format 似乎可以满足我们几乎所有的需求,但是我无法确定的一件事是是否有一种方法可以使用 clang-format 配置命名约定。这可以通过 clang-tidy 使用readability-identifier-naming来完成。

clang-format 中有类似的功能吗?

c++ camelcasing naming-conventions clang-format clang-tidy

5
推荐指数
0
解决办法
3873
查看次数

"它被移动后使用[bugprone-use-after-move]"是否在这里警告了一个真正的问题?

我适应递延PTR香草萨特在cppcon 2016谈及的理念,能够管理在一个更安全的方式一个id为代表的外部资源.

因此,我创建了一个非复制的,只有可移动的类保存id资源应该代表.像unique_ptrid应该成为0如果对象移动到另一个对象.

根据我的理解,即使在被移动之后,如果被调用的函数没有任何先决条件,你仍然可以允许使用该对象,所以根据我的理解,这应该是有效的:

int main() {

    resource src = make_resource(10);
    resource dst;
    std::cout << "src " << src.get() << std::endl;
    std::cout << "dst " << dst.get() << std::endl;

    dst = std::move(src);
    std::cout << "src " << src.get() << std::endl; // (*)
    std::cout << "dst " << dst.get() << std::endl;

    src = make_resource(40);
    std::cout << "src " << src.get() << std::endl;
    std::cout << "dst " << dst.get() << std::endl;

    return 0;
} …
Run Code Online (Sandbox Code Playgroud)

c++ move c++17 clang-tidy

4
推荐指数
2
解决办法
285
查看次数

Clang-Tidy“抑制 X 警告”是什么意思?

这是我的扫描命令(使用 .clang-tidy 配置):

clang-tidy <source_file> -- <a_bunch_of_include_files> <a_bunch_of_libraries>
Run Code Online (Sandbox Code Playgroud)

我看到了源代码的输出,这很好。在最底部我看到:

Suppressed 30000 (30000 in non-user code). warnings
Run Code Online (Sandbox Code Playgroud)

源代码使用多个标头,源文件是一个非常大且复杂的系统的一小部分。clang-tidy 是否也会扫描包含文件和库?这 30,000 个警告从何而来?

c++ clang-tidy

4
推荐指数
1
解决办法
1219
查看次数

为什么 clang-tidy 建议在任何地方添加 [[nodiscard]] ?

我有一个 C++ 项目,clang-tidy建议在[[nodiscard]]任何地方添加。这是一个好习惯吗?我的理解是,[[nodiscard]]只有在忽略返回值对程序可能是致命的时才应该使用。我有一个对象Car,它有一个成员const unsigned int m_ID。吸气剂应该unsigned int getID()[[nodiscard]]吗?clang-tidy 建议如此。

编辑:

当然,我不想忽略吸气剂。但
我的观点是,如果每个返回内容的函数都应该有一个[[nodiscard]],那么该属性[[nodiscard]]无论如何都是多余的。编译器可以简单地检查所有返回内容的函数。

c++ clang clang-tidy

4
推荐指数
2
解决办法
671
查看次数

clang-tidy 抱怨结构中的 std::string

struct Thing {
  std::string something;
};
Run Code Online (Sandbox Code Playgroud)

Clang-tidy 抱怨:
warning: an exception may be thrown in function 'Thing' which should not throw exceptions [bugprone-exception-escape]

我知道该bugprone-exception-escape.FunctionsThatShouldNotThrow设置,但我不知道该放什么来抑制所有包含字符串的结构的警告。

有什么建议么?

c++ clang-tidy

4
推荐指数
1
解决办法
258
查看次数