Difference between revisions of "Turn warnings and hints on or off"
Line 43: | Line 43: | ||
* control all warnings per unit: add <syntaxhighlight>{$warnings off}</syntaxhighlight> or resp. <syntaxhighlight>{$warnings on}</syntaxhighlight> just after the unit name.<br><br> | * control all warnings per unit: add <syntaxhighlight>{$warnings off}</syntaxhighlight> or resp. <syntaxhighlight>{$warnings on}</syntaxhighlight> just after the unit name.<br><br> | ||
* control all hints per unit: add <syntaxhighlight>{$hints off}</syntaxhighlight> or resp. <syntaxhighlight>{$hints on}</syntaxhighlight> just after the unit name.<br><br> | * control all hints per unit: add <syntaxhighlight>{$hints off}</syntaxhighlight> or resp. <syntaxhighlight>{$hints on}</syntaxhighlight> just after the unit name.<br><br> | ||
− | |||
* control specific warnings per unit works the same as above | * control specific warnings per unit works the same as above | ||
− | * control specific warnings per piece of code | + | * 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. | 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. | ||
<syntaxhighlight> | <syntaxhighlight> |
Revision as of 15:17, 21 June 2017
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'.
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 or resp.
{$warnings off}
just after the unit name.{$warnings on}
- control all hints per unit: add or resp.
{$hints off}
just after the unit name.{$hints on}
- control specific warnings per unit works the same as above
- 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.