Enables or disables warnings for selected load-time or run-time conditions that may be indicative of developer errors, such as typos or missing "global" declarations.
#Warn [, WarningType, WarningMode]
This is intended to be a developer tool that helps speed up script development by making such errors easily discoverable.
This feature is conceptually related to Visual Basic's "Option Explicit" setting and Perl's "Strict" pragma, though it only provides added-on warnings, rather than requiring any up-front changes to the script (such as explicit declarations for all variables).
WarningType |
One of the values listed below. If omitted, it defaults to All. All: Apply the given WarningMode to all supported warning types. UseUnsetLocal or UseUnsetGlobal: Warn when a variable is used (i.e. its value is read) before being explicitly assigned a value. This is split into separate warning types for locals and globals because, while the use of an unset local is very likely a developer error, many AutoHotkey scripts intentionally check unset globals by design, so it may make sense to enable this type of warning for locals but disable it for globals. These are run-time warnings; they may be generated at any time while the script is running. UseEnv: Warn when an environment variable is automatically used in place of an empty script variable. This sometimes occurs when an environment variable's name unexpectedly matches a variable used by the script, but will not occur if the script uses #NoEnv (recommended). This is a run-time warning; it may be generated at any time while running the script. LocalSameAsGlobal: Warn when a local variable has the same name as a global variable anywhere in the script. Note that this warning only applies to implicitly introduced local variables - it doesn't apply to variables that are explicitly declared such as |
WarningMode |
One of the values listed below. If omitted, it defaults to MsgBox. MsgBox: A message box describing the warning will be shown (note that once the message box is dismissed, the script will continue as usual). OutputDebug: A description of the warning will be sent to the debugger (if any) for display. For more details, see OutputDebug. Off: The specified warning(s) will be disabled. |
By default, all warnings are off.
Warnings can't be enabled or disabled at run-time; the settings are determined when a script loads. Therefore, the location in the script is not significant (and, like other # directives, #Warn cannot be executed conditionally).
However, the ordering of multiple #Warn directives is significant: the last occurrence that sets a given warning determines the mode for that warning. So, for example, the two statements below have the combined effect of enabling all warnings except UseEnv:
#Warn All #Warn UseEnv, Off EnvSet EnvVar, 1 x := EnvVar ; Okay since #NoEnv has not been used. x := NotAnEnvVar ; Warning.
#Warn All, Off ; Disable all warnings. This is the default state. #Warn ; Enable every type of warning; show each warning in a message box. #Warn UseUnsetLocal, OutputDebug ; Warn when a local variable is used before it's set; send warning to OutputDebug.