18

In Delphi, you can use compiler directives to disable specific warnings, such as

{$WARN USE_BEFORE_DEF OFF}

But when I tried to do that with a specific hint, whose underscore_style_name I got out of the helpfile, the compiler said it doesn't know what {$HINT} is. So is there any way to do this?

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • Why do you want to disable a hint? Things you get hinted on are eliminated by the compiler in the EXE anyway, so you are guaranteed not to affect the program's execution. – JosephStyons Jan 14 '09 at 20:03
  • 3
    Because I don't like generating hints and warnings, and I can see, by reading the code, that the possible condition it's warning me about in this hint doesn't apply in this case. – Mason Wheeler Jan 14 '09 at 20:12
  • I disagree, the hints are most of the time valid. And we have a succesfull 0 hint strategy – Toon Krijthe Jan 14 '09 at 20:55
  • 5
    Yep. Most of the time they are. In this case, though, the compiler doesn't understand that "raise" exits the procedure. – Mason Wheeler Jan 14 '09 at 21:43
  • Yeah, hints are your friend. Don't hate on the hints. – Jim McKeeth Jan 14 '09 at 23:03
  • I would need to see that code you have there Mason. The compiler actually does understand that "raise" exits a procedure in every case I have used it. – Jim McKeeth Jan 14 '09 at 23:05
  • Agree: hints and warnings should be fixed in code, not by ignoring them. – Nick Hodges Jan 14 '09 at 23:55
  • I took a look at Mason's code and showed him how to fix the hint. It was an undefined else condition. I've never met a hint or a warning that I couldn't fix. The compiler is my friend. – Jim McKeeth Jan 15 '09 at 02:23
  • 7
    @everybody_saying_not_to_turn_off_hints: There certainly are compiler warnings and hints that are wrong. Usually it is in complex code where something like this happens: If I initialize a variable it tells me, that the value assigned to it will not be used, if I don't, it tells me that it might not have been initialized. There are other oddities but this one happens to me the most. (that's Delphi 2007, it might have been fixed in later versions) – dummzeuch Aug 10 '11 at 07:28

4 Answers4

31

No specific hints, but you can disable them all.

{$HINTS OFF}
procedure MyProc;
var
  i : integer;
begin
  DoSomething;
end;
{$HINTS ON}
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
7

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".

DiGi
  • 2,528
  • 18
  • 26
  • 1
    Yeah, hints are your friend. Don't hate on the hints. – Jim McKeeth Jan 14 '09 at 23:02
  • 1
    I took a look at Mason's code and showed him how to fix the hint. It was an undefined else condition. I've never met a hint or a warning that I couldn't fix. The compiler is my friend. – Jim McKeeth Jan 15 '09 at 02:27
  • 2
    But ... there are cases where it absolutely makes sense. Just one example: There's little you can do about [H2457](http://docwiki.embarcadero.com/RADStudio/Berlin/en/H2457_Inline_function_%27%25s%27_has_not_been_expanded_because_contained_unit_%27%25s%27_uses_compiling_unit_%27%25s%27_indirectly_(Delphi)) specifically, other than refactor lots of code just in order to have the compiler stop whining. That would be a classic example of disabling that particular one only. Not having that partcular call inlined is not the end of the world, so shut up, Compiler! – JensG May 13 '19 at 14:34
6

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.)

dummzeuch
  • 10,975
  • 4
  • 51
  • 158
4

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.

JosephStyons
  • 57,317
  • 63
  • 160
  • 234