【问题标题】:Reason for failed Cppcheck "Reference to auto variable returned"?Cppcheck“引用自动变量返回”失败的原因?
【发布时间】:2018-08-01 14:31:26
【问题描述】:

我看到以下Cppcheck,但不明白为什么会收到它:

directoryutils.cpp 173 error returnReference false Reference to auto variable returned.

我不明白为什么会返回 auto 变量,也不了解“参考部分”。这是误报吗?

标头:static const FilePerApplication &applicationDataDirectoryMapWithoutCurrentVersion();

const CDirectoryUtils::FilePerApplication &CDirectoryUtils::applicationDataDirectoryMapWithoutCurrentVersion()
{
  static const FilePerApplication dirs = [ = ] {
      FilePerApplication directories;
      for (const QFileInfo &info : CDirectoryUtils::applicationDataDirectories())
      {
        .... fill in data ....
        directories.insert(info.filePath(), appInfo);
      }
      return directories; // LINE 173 why auto???
  }();

  return dirs;
}
  1. 备注:using FilePerApplication = QMap<QString, CApplicationInfo>;
  2. 备注:还给它一个显式返回类型-> FilePerApplication 就像这里https://stackoverflow.com/a/9620143/356726 不会使检查错误静音

【问题讨论】:

  • 显示这段代码所在函数的声明。它的返回类型是什么?
  • 按要求更新
  • “为什么会返回一个自动变量”,因为directories 是一个本地对象。我也不明白“参考部分”。 lambda的返回类型应该是FilePerApplication,看来Cppcheck认为是FilePerApplication&
  • 一般来说,cppcheck 不能很好地处理 lambda。您可以预料到这些类型的误报。这里的auto 表示变量具有自动存储功能。如果我不得不疯狂猜测,引用来自方法的签名......它只是不解析 lambda 并认为 return 语句属于方法......
  • Cppcheck 的现任负责人没有用示例代码报告任何内容。但它可能仍然存在,并且代码不足以重现这一点。

标签: c++ qt cppcheck


【解决方案1】:

Reason for failed Cppcheck "Reference to auto variable returned"? 中的评论很可能是正确的,它是关于“自动存储”的,而且它也是与不正确处理 lambda 相关的误报。

此错误出现在 Cppcheck 1.48 中,并已在 1.54 版本中修复。

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 2017-03-23
    • 1970-01-01
    • 2018-09-19
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    相关资源
    最近更新 更多