No trailing whitespace

# L001

# No trailing whitespace

Description:

Trailing whitespace is any whitespace character (space or tab) that is placed at the end of the source line, after other characters or alone.

The presence of trailing whitespace artificially influences some source code metrics and is therefore discouraged.

As a special case, the trailing whitespace in the otherwise empty lines is allowed provided that the amount of whitespace is identical to the indent in the previous line - this exception is more friendly with less smart editors, but can be switched off by setting non-zero value for the `strict-trailing-space` parameter.

Recognized parameters:

Name | Default | Description
--- | --- | ---
`strict-trailing-space` | `0` | Strict mode for trailing whitespace.

Compliance: Inspirel

Show source code for this rule

Hide source code

    # No trailing whitespace

    set strictMode [getParameter "strict-trailing-space" 0]

    foreach f [getSourceFileNames] {
        set lineNumber 1
        set previousIndent ""
        foreach line [getAllLines $f] {

            if [regexp {^.*[[:space:]]+$} $line] {
                if {$strictMode || [string trim $line] != "" || $line != $previousIndent} {
                    report $f $lineNumber "trailing whitespace"
                }
            }

            regexp {^([[:space:]]*).*$} $line dummy previousIndent
            incr lineNumber
        }
    }

Rule index