How can I temporarily disable the “return value might be undefined” warning?

时光毁灭记忆、已成空白 提交于 2019-12-04 00:59:04

you can't disable this warning globally, but you can use the {$WARN NO_RETVAL OFF} to disable locally the warning.

{$WARN NO_RETVAL OFF}
function TfrmNagScreen.Run: TOption;
begin
  if ShowModal = mrOk then
    Result := TOption(rdgAction.EditValue)
  else
    Abort
end;
{$WARN NO_RETVAL ON}
WileCau

I don't have a Delphi compiler available at the moment, but rearranging the code to remove the if..else might make the warning go away:

function TfrmNagScreen.Run: TOption;
begin
  if ShowModal <> mrOk then
    Abort;

  Result := TOption(rdgAction.EditValue);
end;

See also How to disable a warning in Delphi about “return value … might be undefined”?.

You can use a neat trick to fool the compiler. Define a library function as so:

procedure Abort(var X);
begin
  SysUtils.Abort;
end;

You can then write your function as:

if ShowModal = mrOk then
  Result := TOption(rdgAction.EditValue)
else
  Abort(Result)

The compiler thinks you've written to Result since it's a var parameter and it stops bleating.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!