我正在将代码从Delphi 10 Seattle升级到Delphi 10.2 Tokyo,并在分配时获得了大量H2077提示Value assigned to ... never used
。
(即使在过去明确添加这些内容以摆脱'可能没有价值'警告的地方)。
这些都是初始化的函数,如:
Result := 0;
...
或者:
Result := ftType1; // where ftType1 is an enumerated type
...
编译器在检测这些内容时是否变得更聪明,或者在函数的初始返回值方面发生了哪些变化?
我们总是在'on'上提供这些提示,而且我总是构建(不编译)。
在西雅图没有提示的情况下构建的示例函数(1),
但是在东京的第一条H2077 Value assigned to 'GetDatabaseDialect' not used
行提供了提示Result := 0
。
function GetDatabaseDialect(DBName, User, Pswd: string) : integer;
var
status: array[1..19] of longint;
szDbName, szDbParam: PANSIChar;
dbHandle : pointer;
rslt: longint;
lDPBBuffer : ANSIString;
lDPBLength : integer;
cItem: ANSIChar;
szRslt: PANSIChar; //array[0..IBResultBufferSize-1] of ANSIChar;
begin
Result := 0;
dbHandle := nil;
// init database parameter block with version number
lDPBBuffer := '';
SetLength(lDPBBuffer, 1);
lDPBBuffer[1] := ANSIChar(isc_dpb_version1);
lDPBLength := 1;
// fill Database Parameter Buffer with user name/password
lDPBBuffer := lDPBBuffer +
ANSIChar(isc_dpb_user_name) +
ANSIChar(Length(User)) +
ANSIString( User );
Inc(lDPBLength, 2 + Length(User));
lDPBBuffer := lDPBBuffer +
ANSIChar(isc_dpb_password) +
ANSIChar(Length(Pswd)) +
ANSIString( Pswd );
Inc(lDPBLength, 2 + Length(Pswd));
//Pointers naar naam + buffer
szDbName := PANSIChar(ANSISTring(DBName));
szDbParam := PANSIChar( lDPBBuffer );
// attach to the database and set dialect
rslt := isc_attach_database(@status, 0, szDbName, @dbHandle, lDPBLength, szDbParam);
if rslt <> 0 then
raise EDatabaseError.Create('Error attaching database! ISC# ' + IntToStr(rslt));
//Haal sql dialect op
szRslt := AllocMem(1000);
try
FillChar( szRslt^, 1000, 0);
cItem := ANSIChar( isc_info_db_SQL_dialect );
rslt := isc_database_info(@status, @DBHandle, 1, @cItem, 1000, szRslt);
if rslt <> 0 then
raise EDatabaseError.Create('Error retrieving database info ! ISC# ' + IntToStr(rslt));
Result := Ord(szRslt[3]); //3e positie is dialect
finally
FreeMem(szRslt);
end;
// Drop the connection to the database
rslt := isc_detach_database(@status, @dbHandle);
if rslt <> 0 then
raise EDatabaseError.Create('Error detaching database! ISC# ' + IntToStr(rslt));
end;
来自第三方库的示例(2)似乎没有针对东京进行优化,
用枚举类型说明案例:
H2077 Value assigned to 'TppTemplate.StreamType' not used
请注意,将分配更改为Result := ftASCII;
不会使提示消失(我最初假设它与第一个枚举值相关联)不正确。
type TppFormatType = (ftBinary, ftASCII);
function TppTemplate.StreamType(aStream: TStream): TppFormatType;
var
lSavePos: Integer;
begin
{save stream position}
lSavePos := aStream.Position;
Result := ftBinary;
try
ComputeOffsetFromStream(aStream);
aStream.Seek(FOffset, soBeginning);
if IsValidASCIISignature(aStream) then
Result := ftASCII
else if IsValidBinarySignature(aStream) then
Result := ftBinary
else
raise EInvalidTemplateError.Create(ppLoadStr(49));
finally
{restore stream position}
aStream.Seek(lSavePos, soBeginning);
end;
end; {function, StreamType}
共同点似乎是结果分配在try / finally块中。
答案 0 :(得分:6)
考虑这段代码,只需对场景进行最小程度的再现:
function Bar: Boolean;
begin
Result := Random<0.5;
end;
function Foo: Integer;
begin
Result := 0;
if Bar then
Result := 1
else
raise Exception.Create('');
end;
编译器,即使是旧版本,也会发出以下提示:
[dcc32提示]:H2077分配给'Foo'的值从未使用过
这是合理的。对Result
的第一次分配毫无意义,可以删除。
现在考虑这种变化:
function Foo: Integer;
begin
Result := 0;
try
if Bar then
Result := 1
else
raise Exception.Create('');
finally
end;
end;
旧版本的编译器不再发出提示,但最新版本的编译器会发出提示。对于旧版本,这应该被视为编译器缺陷。上面显示的Foo
的两个变体在语义上是相同的。编译器在生成相同代码时是合理的。
正如您所推测的那样,在try/finally
块内的赋值是触发先前版本中的缺陷所必需的。
我们可以得出结论,Embarcadero开发商已经修复了东京的缺陷。您可以通过删除虚假的初始分配来解决提示。
当然,如果您的代码是由旧版本的编译器以及新版本编译的,那么您就处于绑定状态。使用现在的代码,新版本的编译器会发出提示。删除初始赋值,旧版本的编译器会发出提示。