Sign in Join
Wiki copytemporary

Turn warnings and hints on or off: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11There 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.
22
33``` pascal
44procedure TForm.Button1Click(sender:Tobject);
55begin
66 // Do something that does not use sender
77 Form1.Button1.Caption := 'Test';
88end;
99```
1010
1111This 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 hide them?
1212
1313# Lazarus IDE options
1414
1515The Lazarus IDE can control hints and warnings in two ways:
1616
1717- Globally through the Project Options menu
1818- Using an IDE macro
1919
2020## Global IDE option
2121
2222Go to the Project Options-\>Compiler Options-\>Messages and uncheck 'Hint:Paramater "\$1" not used'.
2323
2424This will turn of this particular hint for your whole project.\
2525If you would like to turn off this hint for all your projects, you can check the left bottom checkbox 'Set compiler options as default'.
2626
2727![](/wiki-images/e/e3/Lazarusglobal.png)
2828
2929## Local IDE option
3030
3131In the Lazarus IDE you can make use of the {%H-} IDE macro to turn hints off for a particular parameter.\
3232Of course {%H+} will turn hints on.
3333
3434``` pascal
3535 procedure FormMouseDown(Sender: TObject; {%H-}Button: TMouseButton;
3636 {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer);
3737```
3838
3939Note that this turns off ALL hints for that parameter, not just the 'is not used' hint, so be careful.
4040
4141# Source code and compiler options
4242
4343The options as described above will only work within the Lazarus IDE.
4444
4545It won't work in any other IDE, not even de FPC textmode IDE. The compiler output will still emit warnings and hints.
4646
4747Luckily there are several other options to control hints and warnings in the source code itself.
4848
4949## Control all warnings per unit:
5050
5151Add
5252
5353``` pascal
5454{$warnings off}
5555```
5656
5757or resp.
5858
5959``` pascal
6060{$warnings on}
6161```
6262
6363just after the unit name.
6464
6565## Control all hints per unit:
6666
6767Add
6868
6969``` pascal
7070{$hints off}
7171```
7272
7373or resp.
7474
7575``` pascal
7676{$hints on}
7777```
7878
7979just after the unit name.
8080
8181## Control specific warnings:
8282
8383Add
8484
8585``` pascal
8686{$warn <number> off}
8787```
8888
8989just after the unit name. See the example below. You can also use this construct if you want all warnings off but some specific warning(s) on:
9090
9191``` pascal
9292{$warnings off}{$warn <number> on}
9393```
9494
9595## Control Specific Warnings or hints per piece of code:
9696
9797You 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.
9898
9999``` pascal
100100procedure withunusedparameter(const testme:integer);
101101{$push}{$warn 5024 off}
102102begin
103103//
104104end;
105105{$pop}
106106begin
107107 withunusedparameter(100);
108108end.
109109```
110110
111111# Obtaining the warnings and hints numbers to suppress
112112
113113You can obtain the needed numbers by compiling your code once with line info on and with -vwhilq
114114
115115This will show both the numbers and the messages and the line numbers where they are found.
116116
117117The other way to obtain hint, notice or warning number is to search inside FP repository for source of your current FPC version.
118118
119119All of the messages with its own description is stored in \<FPC source dir\>/msg/error\<language\>.msg
120120
121121This file is also have header, which provides explanation how error/notice/hint/warning constants are constructed.
122122
123123For the English language it's contains following:
124124
125125``` pascal
126126# CodePage 20127
127127
128128#
129129# The constants are build in the following order:
130130# <part>_<type>_<txtidentifier>
131131#
132132# <part> is the part of the compiler the message is used
133133# asmr_ assembler parsing
134134# asmw_ assembler writing/binary writers
135135# unit_ unit handling
136136# option_ command line parameter parsing
137137# scan_ scanner
138138# parser_ parser
139139# type_ type checking
140140# general_ general info
141141# exec_ calls to assembler, external linker, binder
142142# link_ internal linker
143143# package_ package handling
144144#
145145# <type> the type of the message it should normally used for
146146# f_ fatal error
147147# e_ error
148148# w_ warning
149149# n_ note
150150# h_ hint
151151# i_ info
152152# l_ add linenumber
153153# u_ used
154154# t_ tried
155155# c_ conditional
156156# d_ debug message
157157# x_ executable informations
158158# o_ normal (e.g., "press enter to continue")
159159#
160160# <type> can contain a minus sign at the beginning to mark that
161161# the message is off by default. Look at type_w_explicit_string_cast
162162# for example.
163163```
164164
165165So, to find your warning/notice/etc, you just need to search the message body. For the example, it can be \<unit "Pas2Js.pas" not used in main.pas\>, to find this warning, you need to replace all of the local data in message body with \$\< data number \>,
166166
167-`for this situation it will be: <Unit "$1" not used in $2>;`
167+``` pascal
168+for this situation it will be: <Unit "$1" not used in $2>;
169+```
168170
169171After that, you can find the required message just by finding its body in errors\<language\>.msg file, you will find the following:
170172
171173``` pascal
172174sym_n_unit_not_used=05023_H_Unit "$1" not used in $2
173175% The unit referenced in the \var{uses} clause is not used.
174176```
175177
176178So, the actual notice number is 05023.
177179
178180Based on that number you can decide to suppress the warning or hint globally, per unit or per routine, depending on what you need.
179181
180182For example, if you want to suppress unused parameters you can subsequently compile with -vm5024.
181183
182184With the -vm compiler option you can use multiple warning and hint numbers separated by a comma. Or you can write:
183185
184186``` pascal
185187{$warn 5024 off}
186188```
187189
188190This needs to be done for each number separately.
189191
190192See also:
191193
192194---
193195*From the Free Pascal wiki, page "Turn warnings and hints on or off", as it stood on 2020-03-02 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history