#Warn

Enables or disables warnings for specific conditions which may indicate an error, such as a typo or missing "global" declaration.

#Warn WarningType, WarningMode

Parameters

WarningType

Type: String

[v2.1-alpha.12+]: If omitted, the program-wide default warning mode is changed. This is used by any warning types which are enabled by default or set to On.

[v2.0]: If omitted, it defaults to All. The comma must be present.

Otherwise, specify the type of warning to enable or disable.

VarUnset: Before the script starts to run, display a warning for the first reference to each variable which is never used in any of the following ways:

LocalSameAsGlobal: Before the script starts to run, display a warning for each undeclared local variable which has the same name as a global variable. This is intended to prevent errors caused by forgetting to declare a global variable inside a function before attempting to assign to it. If the variable really was intended to be local, a declaration such as local x or static y can be used to suppress the warning.

This warning is disabled by default.

#Warn
g := 1
ShowG() {       ; The warning is displayed even if the function is never called.
    ;global g   ; <-- This is required to assign to the global variable.
    g := 2
}
ShowG
MsgBox g        ; Without the declaration, the above assigned to a local "g".

Unreachable: Before the script starts to run, show a warning for each line that immediately follows a Return, Break, Continue, Throw or Goto at the same nesting level, unless that line is the target of a label. Any such line would never be executed.

If the code is intended to be unreachable - such as if a return has been used to temporarily disable a block of code, or a hotkey or hotstring has been temporarily disabled by commenting it out - consider commenting out the unreachable code as well. Alternatively, the warning can be suppressed by defining a label above the first unreachable line.

NamedArg: [Keysharp] Before the script starts to run, show a warning for a named argument whose name is not a parameter of the function being called. Also warns when a parameter is supplied both positionally and by name, and when a name refers to something that cannot be supplied as an argument (the variadic parameter, or the object a method is called on).

Only calls whose target can be identified at build time are checked: a function named directly, and constructors of classes defined in the script. A call through a variable or on an object is checked when it runs, since which function that is cannot be known until then. Either way the call raises an error if the name is wrong; this warning only reports it sooner.

The one false positive to expect is a script that reassigns a function name and then calls the replacement by name. Use #Warn NamedArg, Off in that case.

All: Apply the given WarningMode to all supported warning types.

WarningMode

Type: String

If omitted, it defaults to On. Otherwise, specify a value indicating how warnings should be delivered.

MsgBox: Show a message box describing the warning. Note that once the message box is dismissed, the script will continue as usual.

StdOut: Send a description of the warning to stdout (the program's standard output stream), along with the filename and line number. This allows fancy editors such as SciTE to capture warnings without disrupting the script - the user can later jump to each offending line via the editor's output pane.

OutputDebug: Send a description of the warning to the debugger for display. If a debugger is not active, this will have no effect. For more details, see OutputDebug.

On [v2.1-alpha.12+]: Enables the given WarningType using the program-wide default mode, which defaults to MsgBox. A separate #Warn WarningMode directive can be used to change the default mode.

Off: Disable warnings of the given WarningType.

UTF-8 [v2.1-alpha.13+]

A warning is shown if a script file being read as UTF-8 contains one or more bytes which are not part of a valid UTF-8 byte sequence. These bytes cannot be decoded to a character, so are replaced with '�'. This usually indicates that the script file was not saved as UTF-8. By default, AutoHotkey expects files which contain non-ASCII characters to be encoded as UTF-8. To resolve the issue, save the file as UTF-8 or remove any invalid byte sequences.

This warning uses the default output mode, and cannot be disabled except by setting the default mode to Off.

Remarks

If this directive is unspecified in the script, all warnings are enabled and use the MsgBox mode, except for LocalSameAsGlobal, which is disabled. NamedArg is enabled by default.

[v2.1-alpha.12+]: Each module has its own warning settings. Modules defined with #Module default to the same settings as the previous module. For the default module and modules loaded by #Import, VarUnset and Unreachable default to On and LocalSameAsGlobal defaults to Off.

[v2.1-alpha.12+]: A program-wide default mode can be set by using #Warn WarningMode, with any mode except On. The last such directive affects all warnings which are enabled by default, and all warnings for which WarningMode was set to On. #Warn Off causes those warnings to be ignored. The following special cases also exist:

The checks which produce VarUnset, LocalSameAsGlobal and Unreachable warnings are performed after all directives have been parsed, but before the script executes. Therefore, the location within a module is not significant (and, like other directives, #Warn cannot be executed conditionally).

However, the ordering of multiple #Warn directives is significant when WarningType is specified: 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 LocalSameAsGlobal (which is also the default configuration):

#Warn All
#Warn LocalSameAsGlobal, Off

Local and Global Variables

Examples

Disables all warnings for the current module. Not recommended.

#Warn All, Off

Enables every type of warning for the current module. [v2.1-alpha.12+]: Uses the program-wide default mode.

#Warn All

For v2.1, sets only the default warnings to be enabled with the program-wide default mode. For v2.0, enables all warnings and sets mode to MsgBox.

#Warn

Sends a warning to OutputDebug for each undeclared local variable which has the same name as a global variable.

#Warn LocalSameAsGlobal, OutputDebug

[v2.1-alpha.12+]: Causes warnings to be delivered via StdOut, unless a mode has been specified for the warning.

#Warn StdOut