函数初始化代码从西雅图更改为东京了吗? [英] Has function initialization code changed from Seattle to Tokyo?
问题描述
我正在将代码从Delphi 10 Seattle升级到Delphi 10.2 Tokyo,并获得很多H2077提示赋值给...从未使用的值
。
(即使在过去为了避免可能没有值警告而明确添加的地方)。
I am in the process of upgrading code from Delphi 10 Seattle to Delphi 10.2 Tokyo and get a lot of H2077 hints Value assigned to ... never used
on assignments.
(Even in places where these were explicitly added in the past to get rid of 'may not have a value' warnings).
这些所有函数都初始化为:
These are all function initialized like:
Result := 0;
...
或:
Result := ftType1; // where ftType1 is an enumerated type
...
编译器变得更聪明了吗
我们总是将这些提示打开,而我始终在构建(而不是编译)。
We have always had these hints 'on', and I always build (not compile).
p>
示例函数(1)在西雅图没有提示地构建,
但给出提示 H2077分配给'GetDatabaseDialect'的值东京第一行
。结果:= 0
上未使用
Example function (1) that builds without hints in Seattle,
but gives the hint H2077 Value assigned to 'GetDatabaseDialect' not used
on the first Result := 0
line in Tokyo.
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未使用分配给'TppTemplate.StreamType'的值
请注意,将赋值更改为 Result:= ftASCII;
不会使提示消失(我最初的假设是先与关联枚举值不正确。)
Example (2) from a third party library that does not seem to be optimized for Tokyo,
illustrating the case with enumerated types:
H2077 Value assigned to 'TppTemplate.StreamType' not used
Note that changing the assignment to Result := ftASCII;
does not make the hint go away (my initial assumption that it was associated with the first enumeration value was incorrect).
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块中。
The common denominator seems to be the Result assignments being in try/finally blocks.
推荐答案
请考虑以下代码,以尽量简化您的方案:
Consider this code with a minimal reproduction of your scenario:
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;
即使是较旧的版本,编译器也会发出以下提示:
The compiler, even older versions, emits the following hint:
[dcc32提示]:H2077分配给'Foo'从未使用过的值
[dcc32 Hint]: H2077 Value assigned to 'Foo' never used
这是合理的。 Result
的第一个分配是没有意义的,可以删除。
This is reasonable. The first assignment to Result
is pointless and can be removed.
现在考虑这种变化:
function Foo: Integer;
begin
Result := 0;
try
if Bar then
Result := 1
else
raise Exception.Create('');
finally
end;
end;
较早版本的编译器不再发出提示,但是最新版本的编译器会发出提示。对于较旧的版本,应将其视为编译器缺陷。上面显示的 Foo
的两个变体在语义上是相同的。
Older versions of the compiler no longer emit the hint, but the latest version of the compiler does. This should be considered a compiler defect, for older versions. The two variants of Foo
shown above are semantically identical. The compiler would be justified in generating identical code.
您猜想,赋值在 try / finally
内部
我们可以得出结论,Embarcadero开发人员已在东京修复了一个缺陷。您可以通过删除虚假的初始分配来解决提示。
We can conclude that the Embarcadero developers have fixed a defect in Tokyo. You can resolve the hints by removing the spurious initial assignments.
当然,如果您的代码将由较早版本的编译器以及新版本进行编译,则您将处于绑定状态。使用现在的代码,编译器的新版本会发出提示。删除初始分配,编译器的旧版本会发出提示。
Of course, if your code is to be compiled by older versions of the compiler, as well as by new versions, then you are in a bind. With the code as it stands now, a hint is emitted by new versions of the compiler. Remove the initial assignment and a hint is emitted by old versions of the compiler.
这篇关于函数初始化代码从西雅图更改为东京了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!