小さい頃はエラ呼吸

いつのまにやら肺で呼吸をしています。


【cppcheck】Variable 'xxx' is reassigned a value before the old one has been used.

はじめに

C++の静的解析ツール「cppcheck」でソースコードを静的解析した場合に、以下の警告がでることがあります。

Variable 'xxx' is reassigned a value before the old one has been used.

cppcheckのバージョン
  • v1.64
サンプルプログラム

以下のソースプログラムを解析にかけると表示されます。

#include "stdafx.h"
#include <Windows.h>

int func1();
int func2();

int _tmain(int argc, _TCHAR* argv[])
{
  int ret;
  ret = func1();

  ret = func2();
  if (ret != 0)
  {
    // エラー処理
  }
  return 0;
}

関数の戻り値を格納するための変数retが使われる前に、新しい値で上書きされるとこの警告が表示されます。要するに値を使わなくて良いの?と教えてくれているんですね。