Skip to content

whitespace/operators false positive on U/u integer-literal shift (10U<<20) #439

Description

@Noethix55555

Description

cpplint allows shifting integer literals without spaces around << (for example 10<<20), and to support suffixed literals it strips a trailing integer-literal suffix in CheckOperatorSpacing (cpplint.py:4536):

match = re.search(r"(operator|[^\s(<])(?:L|UL|LL|ULL|l|ul|ll|ull)?<<([^\s,=<])", line)

The suffix alternation includes UL, LL, ULL, ul, ll, ull but omits the standalone unsigned suffix U/u. So 10U<<20 and 10u<<20 are reported as whitespace/operators "Missing spaces around <<", while 10UL<<20, 10LL<<20, 10ULL<<20, 10L<<20, and 10<<20 are all accepted. This is a false positive. The existing tests for 10LL<<20 and 10ULL<<20 (cpplint_unittest.py) confirm the intent is to accept suffixed integer-literal shifts.

Steps to Reproduce

shift.cc:

unsigned a = 10U<<2;
unsigned b = 10UL<<2;
unsigned c = 10<<2;
unsigned d = 10u<<2;
python cpplint.py --filter=-legal/copyright shift.cc

Observed:

shift.cc:1:  Missing spaces around <<  [whitespace/operators] [3]
shift.cc:4:  Missing spaces around <<  [whitespace/operators] [3]

Only the U/u variants are flagged.

Expected Behavior

No whitespace/operators warning for any of these literal-on-literal shifts.

Fix

Add U and u to the suffix alternation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions