Comma should not be preceded by whitespace, but should be followed by one

# T009

# Comma should not be preceded by whitespace, but should be followed by one

Description:

A comma, whether used as operator or in various lists, should not be preceded by whitespace on its left side, but should be followed by whitespace on its right side:

    void fun(int x, int y, int z);
    int a[] = {5, 6, 7};
    class A : public B,
              public C
    {
         // ...
    };

An exception to this rule is allowed for `operator,`:

    struct A {};
    void operator,(const A &left, const A &right);

Compliance: Inspirel

Show source code for this rule

Hide source code

    # Comma should not be preceded by whitespace, but should be followed by one

    foreach f [getSourceFileNames] {
        foreach t [getTokens $f 1 0 -1 -1 {comma}] {
            set line [lindex $t 1]
            set column [lindex $t 2]
            set preceding [getTokens $f $line 0 $line $column {}]
            if {$preceding == {}} {
                report $f $line "comma should not be preceded by whitespace"
            } else {
                set lastPreceding [lindex [lindex $preceding end] 3]
                if {$lastPreceding == "space"} {
                    report $f $line "comma should not be preceded by whitespace"
                }
            }
            set following [getTokens $f $line [expr $column + 1] [expr $line + 1] -1 {}]
            if {$following != {}} {
                set firstFollowing [lindex [lindex $following 0] 3]
                if {$firstFollowing != "space" && $firstFollowing != "newline" &&
                    !($lastPreceding == "operator" && $firstFollowing == "leftparen")} {
                    report $f $line "comma should be followed by whitespace"
                }
            }
        }
    }

Rule index