安装C/C++
插件:https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
然后在设置中将这两项勾上,使得clang-tidy
能够自动运行:
C_Cpp › Code Analysis › Clang Tidy: Enabled
C_Cpp › Code Analysis: Run Automatically
默认似乎是不做任何检查的。需要在C_Cpp › Code Analysis › Clang Tidy › Checks: Enabled
中启用需要的检查,各种检查的文档可以在这里找到:https://clang.llvm.org/extra/clang-tidy/checks/list.html。建议开启的检查:
bugprone-use-after-move
: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/use-after-move.htmlbugprone-narrowing-conversions
: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/narrowing-conversions.html
举个例子,只启用bugprone-use-after-move
,然后创建一个vscode工程:
#include <iostream>
#include <vector>
#include <string>
int main() {
std::string str = "Hello, world!\n";
std::vector<std::string> messages;
.emplace_back(std::move(str));
messagesstd::cout << str;
return 0;
}
效果: