5

古いプロジェクトをDelphiXEに移植していますが、次のコードでこの警告が表示されます。

function RemoveThousandSeperator(Text: String) : String;
Var P : Integer;
begin
  if length(Text) > 3 then begin
    p := Pos(FormatSettings.ThousandSeparator,Text);
   while p >0 do begin
      Delete(Text,p,1);
      p := Pos(FormatSettings.ThousandSeparator,Text);
    end;
  end;
  result := Text;
end;

FormatSettings.ThousandSeparatorでさえchar型です。

LE:なぜこの警告が発生するのか誰かに教えてもらえないかと尋ねています。コードは古く、作り直されます。

LE2:この警告を取得するには、Delphiコンパイラですべての警告をtrueに設定する必要があります-ヒントと警告

LE3:誰かがそれを必要とする場合-{$ WARNUNSAFE_CASTOFF}は警告をオフにします。

LE4:警告が信じがたいと信じている人のための警告のスクリーンショット

ここに画像の説明を入力してください

4

1 に答える 1

6

警告の原因は、次のFormatSettings変数の宣言SysUtils.pasです。

var
  // Note: Using the global FormatSettings variable corresponds to using the
  // individual global formatting variables and is not thread-safe.
  FormatSettings: TFormatSettings absolute CurrencyString;

文字列()をキャストして( CurrencyString)を記録しTFormatSettingsます。

したがって、警告はSysUtils.pasコードで生成されますが、警告を生成する問題は、投稿したコードではなく、にあります。


テストケース(Delphi XE)は次のとおりです。

program Project1;

{$APPTYPE CONSOLE}
{$WARN UNSAFE_CAST ON}

type
  TTest = record
    FS: string;
  end;

var
  Str: string;
  Test: TTest absolute Str;

begin
  Str:= 'abc';
  Writeln(Test.FS);   //  W1048 Unsafe typecast of 'string' to 'TTest'
end.
于 2012-11-01T10:53:51.120 に答える