Skip to main content

warn

warn Function

Syntax:

warn datum &rest arguments → nil

Arguments and Values:

datum, argumentsdesignators for a condition of default type simple-warning.

warn

Description:

Signals a condition of type warning. If the condition is not handled, reports the condition to error output.

The precise mechanism for warning is as follows:

The warning condition is signaled

While the warning condition is being signaled, the muffle-warning restart is established for use by a handler . If invoked, this restart bypasses further action by warn, which in turn causes warn to immediately return nil.

If no handler for the warning condition is found

If no handlers for the warning condition are found, or if all such handlers decline, then the condition is reported to error output by warn in an implementation-dependent format.

nil is returned

The value returned by warn if it returns is nil.

Examples:

(defun foo (x) 
(let ((result (\* x 2)))
(if (not (typep result ’fixnum))
(warn "You’re using very big numbers."))
result))
→ FOO
(foo 3)
6
(foo most-positive-fixnum)
▷ Warning: You’re using very big numbers.
4294967294
(setq \*break-on-signals\* t)
→ T
(foo most-positive-fixnum)
▷ Break: Caveat emptor.
▷ To continue, type :CONTINUE followed by an option number.
▷ 1: Return from Break.
▷ 2: Abort to Lisp Toplevel.
▷ Debug> :continue 1

▷ Warning: You’re using very big numbers.
4294967294

Side Effects:

A warning is issued. The debugger might be entered.

Affected By:

Existing handler bindings.

*break-on-signals*, *error-output*.

Exceptional Situations:

If datum is a condition and if the condition is not of type warning, or arguments is non-nil, an error of type type-error is signaled.

If datum is a condition type, the result of (apply #’make-condition datum arguments) must be of type warning or an error of type type-error is signaled.

See Also:

*break-on-signals*, muffle-warning, signal

Expanded Reference: warn

Basic Warning

warn signals a condition of type warning. If no handler handles it, the warning message is printed to *error-output* and warn returns nil.

(warn "The configuration file is missing.")
WARNING: The configuration file is missing.

=> NIL

Warning with Format Arguments

Like error, warn accepts format control strings with arguments.

(warn "Variable ~S has suspicious value ~S" 'x -1)
WARNING: Variable X has suspicious value -1

=> NIL

Muffling a Warning

A handler can invoke the muffle-warning restart to suppress the warning output entirely.

(handler-bind ((warning (lambda (c)
(declare (ignore c))
(muffle-warning))))
(warn "This warning will not be printed.")
:done)

=> :DONE

Collecting Warnings

You can capture warnings instead of printing them, using handler-bind and the muffle-warning restart.

(let ((collected '()))
(handler-bind ((warning (lambda (c)
(push (format nil "~A" c) collected)
(muffle-warning))))
(warn "First issue")
(warn "Second issue"))
(nreverse collected))

=> ("First issue" "Second issue")

Warning with a Condition Type

You can pass a warning condition type symbol instead of a string.

(define-condition deprecation-warning (warning)
((name :initarg :name :reader deprecation-name))
(:report (lambda (c stream)
(format stream "~S is deprecated." (deprecation-name c)))))

(handler-bind ((deprecation-warning
(lambda (c)
(declare (ignore c))
(muffle-warning))))
(warn 'deprecation-warning :name 'old-function)
:ok)

=> :OK