Pregunta

Quiero deshabilitar una advertencia específica (W1035) en mi código, ya que creo que el compilador está equivocado sobre esta advertencia:

function TfrmNagScreen.Run: TOption;
begin
  if ShowModal = mrOk then
    Result := TOption(rdgAction.EditValue)
  else
    Abort
end;

No hay forma de que el resultado pueda ser indefinido, ya que Abort lanza EAbort.

Lo intenté:

  • {$WARN 1035 Off}: Aparentemente esto solo funciona para algunos errores específicos (ver Documentación)
  • {$W-1035}: No hace nada

Sé que puedo apagar la advertencia a nivel mundial en las opciones del proyecto, o usar {$WARNINGS OFF}, pero eso no es lo que se pretende aquí.

EDITAR: He qc'ed esto ahora como #89744.

¿Fue útil?

Solución

No puede deshabilitar esta advertencia a nivel mundial, pero puede usar el {$WARN NO_RETVAL OFF} para deshabilitar localmente la advertencia.

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

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top