Can specific Delphi hints be disabled?

跟風遠走 提交于 2019-12-01 02:34:58

No specific hints, but you can disable them all.

{$HINTS OFF}
procedure MyProc;
var
  i : integer;
begin
  DoSomething;
end;
{$HINTS ON}

Little off-topic: You should take care about compiler's hints and warnings. They are not just for fun. Compiler is just saying "program may work differently that you think because YOUR source code is not exact".

To play it really safe, one would like to do something like this:

function TopazGetText(const _s: string): string;
begin
{$IFOPT <something>+}
{$DEFINE HINTS_WERE_ON}
{$HINTS OFF}
{$ELSE}
{$UNDEF HINTS_WERE_ON}
{$ENDIF}
  Result := dzDGetText(_s, TOPAZ_TRANSLATION_DOMAIN);
{$IFDEF HINTS_WERE_ON}
{$HINTS ON}
{$ENDIF}
end;

Unfortunately there seems to be no compiler directive for checking whether hints are off or not, so you can't do this. (H+ is not for hints but for long strings). Also, HINTS OFF / ON does not work within a function/procedure.

So you end up turning hints off and on unconditionally for the whole function:

{$HINTS OFF}
function TopazGetText(const _s: string): string;
begin
  Result := dzDGetText(_s, TOPAZ_TRANSLATION_DOMAIN);
end;
{$HINTS ON}

(The compiler used to tell me that it could not inline dzDGetText which is something I don't care about in this case and I don't want to see the hint because this would stop me (and my coworkers) to care about important hints.)

Best I can think of is to surround the subject of the hint with a conditional define, and use the same conditional define around the code that may or may not be needed, as shown below:

If you have this:

procedure MyProc;
var
  i : integer;
begin
  DoSomething;
  //SomethingWith_i_IsCommentedOut;
end;

You will get: Hint: variable "i" is declared but never used

So try this instead:

procedure MyProc;
  {$IFDEF USE_THE_I_PROCEDURE}
var
  i : integer;
  {$ENDIF}
begin
  DoSomething;
  {$IFDEF USE_THE_I_PROCEDURE}
  SomethingWith_i_IsCommentedOut;
  {$ENDIF}
end;

Now you can turn the define on or off, and you should never get a hint.

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