我正在将一个旧项目移植到Delphi XE,我在下面的代码中收到此警告.
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;
Run Code Online (Sandbox Code Playgroud)
甚至FormatSettings.ThousandSeparator的类型为char.
LE:我问是否有人可以告诉我为什么会出现此警告.代码很旧,它将被重新制作.
LE2:为了获得此警告,需要在Delphi编译器提示和警告中将所有警告设置为true
LE3:如果有人需要它 - {$ WARN UNSAFE_CAST OFF}使警告响起.
LE4:对那些认为警告难以置信的人的警告截图

警告的起源是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;
Run Code Online (Sandbox Code Playgroud)
将string(CurrencyString)强制转换为record(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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
986 次 |
| 最近记录: |