Sign in Join
Wiki copytemporary

Turn warnings and hints on or off: what changed

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

Title: Turn warnings and hints on or off

OldNewLine
1+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.
2+
3+``` pascal
4+procedure TForm.Button1Click(sender:Tobject);
5+begin
6+ // Do something that does not use sender
7+ Form1.Button1.Caption := 'Test';
8+end;
9+```
10+
11+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 hide them?
12+
13+# Lazarus IDE options
14+
15+The Lazarus IDE can control hints and warnings in two ways:
16+
17+- Globally through the Project Options menu
18+- Using an IDE macro
19+
20+## Global IDE option
21+
22+Go to the Project Options-\>Compiler Options-\>Messages and uncheck 'Hint:Paramater "\$1" not used'.
23+
24+This will turn of this particular hint for your whole project.\
25+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'.
26+
27+![](/wiki-images/e/e3/Lazarusglobal.png)
28+
29+## Local IDE option
30+
31+In the Lazarus IDE you can make use of the {%H-} IDE macro to turn hints off for a particular parameter.\
32+Of course {%H+} will turn hints on.
33+
34+``` pascal
35+ procedure FormMouseDown(Sender: TObject; {%H-}Button: TMouseButton;
36+ {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: Integer);
37+```
38+
39+Note that this turns off ALL hints for that parameter, not just the 'is not used' hint, so be careful.
40+
41+# Source code and compiler options
42+
43+The options as described above will only work within the Lazarus IDE.
44+
45+It won't work in any other IDE, not even de FPC textmode IDE. The compiler output will still emit warnings and hints.
46+
47+Luckily there are several other options to control hints and warnings in the source code itself.
48+
49+## Control all warnings per unit:
50+
51+Add
52+
53+``` pascal
54+{$warnings off}
55+```
56+
57+or resp.
58+
59+``` pascal
60+{$warnings on}
61+```
62+
63+just after the unit name.
64+
65+## Control all hints per unit:
66+
67+Add
68+
69+``` pascal
70+{$hints off}
71+```
72+
73+or resp.
74+
75+``` pascal
76+{$hints on}
77+```
78+
79+just after the unit name.
80+
81+## Control specific warnings:
82+
83+Add
84+
85+``` pascal
86+{$warn <number> off}
87+```
88+
89+just 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:
90+
91+``` pascal
92+{$warnings off}{$warn <number> on}
93+```
94+
95+## Control Specific Warnings or hints per piece of code:
96+
97+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.
98+
99+``` pascal
100+procedure withunusedparameter(const testme:integer);
101+{$push}{$warn 5024 off}
102+begin
103+//
104+end;
105+{$pop}
106+begin
107+ withunusedparameter(100);
108+end.
109+```
110+
111+# Obtaining the warnings and hints numbers to suppress
112+
113+You can obtain the needed numbers by compiling your code once with line info on and with -vwhilq
114+
115+This will show both the numbers and the messages and the line numbers where they are found.
116+
117+The other way to obtain hint, notice or warning number is to search inside FP repository for source of your current FPC version.
118+
119+All of the messages with its own description is stored in \<FPC source dir\>/msg/error\<language\>.msg
120+
121+This file is also have header, which provides explanation how error/notice/hint/warning constants are constructed.
122+
123+For the English language it's contains following:
124+
125+``` pascal
126+# CodePage 20127
127+
128+#
129+# The constants are build in the following order:
130+# <part>_<type>_<txtidentifier>
131+#
132+# <part> is the part of the compiler the message is used
133+# asmr_ assembler parsing
134+# asmw_ assembler writing/binary writers
135+# unit_ unit handling
136+# option_ command line parameter parsing
137+# scan_ scanner
138+# parser_ parser
139+# type_ type checking
140+# general_ general info
141+# exec_ calls to assembler, external linker, binder
142+# link_ internal linker
143+# package_ package handling
144+#
145+# <type> the type of the message it should normally used for
146+# f_ fatal error
147+# e_ error
148+# w_ warning
149+# n_ note
150+# h_ hint
151+# i_ info
152+# l_ add linenumber
153+# u_ used
154+# t_ tried
155+# c_ conditional
156+# d_ debug message
157+# x_ executable informations
158+# o_ normal (e.g., "press enter to continue")
159+#
160+# <type> can contain a minus sign at the beginning to mark that
161+# the message is off by default. Look at type_w_explicit_string_cast
162+# for example.
163+```
164+
165+So, 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 \>,
166+
167+`for this situation it will be: <Unit "$1" not used in $2>;`
168+
169+After that, you can find the required message just by finding its body in errors\<language\>.msg file, you will find the following:
170+
171+``` pascal
172+sym_n_unit_not_used=05023_H_Unit "$1" not used in $2
173+% The unit referenced in the \var{uses} clause is not used.
174+```
175+
176+So, the actual notice number is 05023.
177+
178+Based on that number you can decide to suppress the warning or hint globally, per unit or per routine, depending on what you need.
179+
180+For example, if you want to suppress unused parameters you can subsequently compile with -vm5024.
181+
182+With the -vm compiler option you can use multiple warning and hint numbers separated by a comma. Or you can write:
183+
184+``` pascal
185+{$warn 5024 off}
186+```
187+
188+This needs to be done for each number separately.
189+
190+See also:
191+
192+---
193+*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