Can specific Delphi hints be disabled?
Asked Answered
I

4

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?

Isocline answered 14/1, 2009 at 19:59 Comment(9)
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.Cymbiform
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.Isocline
I disagree, the hints are most of the time valid. And we have a succesfull 0 hint strategyAntevert
Yep. Most of the time they are. In this case, though, the compiler doesn't understand that "raise" exits the procedure.Isocline
Yeah, hints are your friend. Don't hate on the hints.Scleroderma
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.Scleroderma
Agree: hints and warnings should be fixed in code, not by ignoring them.Caecum
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.Scleroderma
@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)Labannah
A
31

No specific hints, but you can disable them all.

{$HINTS OFF}
procedure MyProc;
var
  i : integer;
begin
  DoSomething;
end;
{$HINTS ON}
Aspersorium answered 14/1, 2009 at 20:42 Comment(1)
Yeah, that's what I was afraid of.Isocline
B
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".

Began answered 14/1, 2009 at 22:3 Comment(3)
Yeah, hints are your friend. Don't hate on the hints.Scleroderma
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.Scleroderma
But ... there are cases where it absolutely makes sense. Just one example: There's little you can do about H2457 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!Empery
L
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.)

Labannah answered 10/8, 2011 at 7:44 Comment(0)
C
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.

Cymbiform answered 14/1, 2009 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.