Turn warnings and hints on or off

From Lazarus wiki
Jump to navigationJump to search

There are several ways to control the behavior of warnings in both the Lazarus IDE and in the source code itself. Some warnings and hints are not very useful in a particular case so you want to hide them from view. On the other hand, if you would hide warnings or hints globally, you may want to turn a particular warning or hint explicitly on for a particular piece of your code.

procedure TForm.Button1Click(sender:Tobject);
begin
  // DoSomethingThat does not use sender
  Form1.Button1.Caption := 'Test';  
end;

This will generate a hint that parameter sender is not used. Doh. I can see that!. Problem is that this hint turns up all over the place and I know what it means. How do I get rid if it?

Lazarus IDE options

The Lazarus IDE can control hints and warnings in two ways:

  • Globally through the Project Options menu
  • Using an IDE macro


Global IDE option

Go to the Project Options->Compiler Options->Messages and uncheck 'Hint:Paramater "$1" not used'.
This will turn of this particular hint for your whole project.
If you would like to turn off this hint for all your projects, you can check the left bottom checkbox 'Set compiler options as default'.


lazarusglobal.png

Local IDE option

In the Lazarus IDE you can make use of the {%H-} IDE macro to turn hints off for a particular parameter.
Of course {%H+} will turn hints on.

    procedure FormMouseDown(Sender: TObject; {%H-}Button: TMouseButton;
      {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer);


Note that this turns off ALL hints for that parameter, not just the 'is not used' hint, so be careful.

Source code options

The options as described above will only work within the Lazarus IDE.
It won't work in any other IDE, not even de FPC textmode IDE. The compiler output will still emit warnings and hints
Luckily there are several other options to control hints and warnings in the source code itself.

  • control all warnings per unit: add
    {$warnings off}
    or resp.
    {$warnings on}
    just after the unit name.

  • control all hints per unit: add
    {$hints off}
    or resp.
    {$hints on}
    just after the unit name.

  • control specific warnings: Add
    {$warn <number> off}
    just after the unit name. See the example below.
  • control specific warnings or hints per piece of code

You can control this very finely grained by using {$push} and {$pop} around a block of code. This does not have to be a method body as long it is a block, e.g. begin/end,repeat/until,/while do, try/finally etc.

procedure withunusedparameter(const testme:integer);
{$push}{$warn 5024 off}
begin
//
end;
{$pop}
begin
  withunusedparameter(100);
end.

Obtaining the warnings and hints numbers to suppress

You can obtain the needed numbers by compiling your code once with line info on and with -vwhilq
This will show both the numbers and the messages and the line numbers where they are found.
Based on that number you can decide to suppress the warning or hint globally, per unit or per routine, depending on what you need.
For example, if you want to suppress unused parameters you can subsequently compile with -vm5024.
With the -vm compiler option you can use multiple warning and hint numbers separated by a comma. Or you can write:

{$warn 5024 off}

This needs to be done for each number separately.