# #Warn [v1.0.95.00+]

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

  The type of warning to enable or disable. If omitted, it defaults to All.

  UseUnsetLocal or UseUnsetGlobal: Warn when a variable is read without having previously been assigned a value or initialized with VarSetCapacity(). If the variable is intended to be empty, assign an empty string to suppress this warning.

  This is split into separate warning types for locals and globals because it is more common to use a global variable without prior initialization, due to their persistent and script-wide nature. For this reason, some script authors may wish to enable this type of warning for locals but disable it for globals.

      #Warn
      ;y := ""  ; This would suppress the warning.
      x := y    ; y hasn't been assigned a value.

  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. This warning occurs when the variable is accessed, but never occurs if the script enables #NoEnv (recommended for multiple reasons).

      #Warn
      ;#NoEnv             ; Add this if "temp" is not intended to be an environment variable.
      ;EnvGet temp, TEMP  ; This would copy the environment variable's value into the script variable.
      temp := ""          ; Despite this line, temp still seems to have a value.
      MsgBox % temp       ; This accesses the environment variable named "TEMP".

  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 access 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 never shown for variables inside a force-local function.

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

  ClassOverwrite [v1.1.27+]: Before the script starts to run, show a warning for each assignment targetting a class variable. For example, `box := new Box` will show a warning if Box is a class, since this would overwrite the class (within the super-global variable, Box). Warnings are also shown for output variables of commands, but not ByRef parameters. Warnings are not shown for nested classes or dynamic variable references.

  Unreachable [v1.1.33+]: Before the script starts to run, show a warning for each line that immediately follows a `Return`, `Break`, `Continue`, `Throw`, `Goto` or `Exit` at the same nesting level, unless that line is the target of a label. Although this does not detect all unreachable code, it detects common errors such as:

  - Placing initialization code (such as an assignment or GroupAdd) in between hotkey subroutines, when it should be in the auto-execute section.
  - Placing the first line of a "multi-line" hotkey subroutine on the same line as the hotkey label. Doing so will make it a single-line hotkey, with an implicit `return` that prevents any subsequent lines from executing. Similarly, placing `return` after a single-line hotkey is redundant and will cause a warning.
  - Placing a subroutine inside another subroutine, breaking the flow of code.

  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.

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

WarningMode

  A value indicating how warnings should be delivered. If omitted, it defaults to MsgBox.

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

  StdOut [v1.1.04+]: 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.

  Off: Disable warnings of the given WarningType.

## Remarks

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.

## Related

Local and Global Variables

## Examples

#1

    #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.