Horst Walter
Horst Walter

Reputation: 14081

Reason for failed Cppcheck "Reference to auto variable returned"?

I see the following Cppcheck, but do not understand why I am getting it:

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

Neither I understand why an auto variable would be returned, nor the "reference part". Is that a false positive?

header: 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. Remark: using FilePerApplication = QMap<QString, CApplicationInfo>;
  2. Remark: Also giving it an explicit return type -> FilePerApplication like here https://stackoverflow.com/a/9620143/356726 does not silence the check error

Upvotes: 2

Views: 526

Answers (1)

Firewave
Firewave

Reputation: 411

The comment in Reason for failed Cppcheck "Reference to auto variable returned"? is most likely correct that it is about "automatic storage" and also it being a false positive related to incorrect handling of the lambda.

This error appeared in Cppcheck 1.48 and was fixed in version 1.54.

Upvotes: 1

Related Questions