我正在将一个旧项目移植到Delphi XE,我在下面的代码中收到此警告. function RemoveThousandSeperator(Text: String) : String;Var P : Integer;begin if length(Text) 3 then begin p := Pos(FormatSettings.ThousandSeparator,Text
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 Compiler-Hints&中设置为true.警告
LE3:如果有人需要它 – {$WARN UNSAFE_CAST OFF}使警告响起.
LE4:对那些认为警告难以置信的人的警告截图
警告的起源是SysUtils.pas中FormatSettings变量的声明: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.
