如何防止不正确的clang-tidy:readability-redundant-string-cstr检测?[复制]

How to prevent incorrect clang-tidy:readability-redundant-string-cstr detection? [duplicate]

提问人:malat 提问时间:5/2/2023 最后编辑:malat 更新时间:5/2/2023 访问量:89

问:

这是我希望在 clang-tidy 中处理的典型(伪)代码(例如,也在这里),用于将可选的以 null 结尾的固定大小缓冲区传达给:std::string

% cat str.cc
#include <iostream>
#include <string>

// read byte buffer from file, at most 4 bytes read:
static void read_buf4(char buf[4])
{
  buf[0] = 'A';
  buf[1] = 'B';
  buf[2] = 'C';
  // buf[3] = 'D';
  buf[3] = 0;
}

int main()
{
  char buf[4];
  read_buf4(buf);
  std::string owner = std::string(buf,sizeof buf).c_str();
  // I want owner.size() to return strnlen(buf,4):
  std::cout << owner << "/" << owner.size() << std::endl;
  return 0;
}

帮助 clang-tidy 理解代码的最可接受的解决方案是什么?

目前报告:

% clang-tidy --checks="-*,readability-redundant-string-cstr" str.cc
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "str.cc"
No compilation database found in /tmp 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.
1 warning generated.
/tmp/str.cc:17:23: warning: redundant call to 'c_str' [readability-redundant-string-cstr]
  std::string owner = std::string(buf,sizeof buf).c_str();
                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                      std::string(buf,sizeof buf)
C++ 字符串 可移植性 clang-tidy

评论


答: 暂无答案