Menu

Bug or not? Enums won't cause "The scope of the variable 'n' can be reduced"

2021-07-16
2021-07-17
  • Jens Schweikhardt

    hello, world\n

    Using 2.5 (going at least back to 1.85), I observed this false-negative:

    $ cat x.c
    typedef enum { FOO, BAR, BAZ } myenum;
    typedef int myint;
    
    int main(void) {
        enum { A, B, C } en;
        myenum e;
        myint i;
        int n;
    
        for (;;) {
            en = A;
            call(en);
            e = BAZ;
            call(e);
            i = 42;
            call(i);
            n = 42;
            call(n);
        }
    }
    

    cppcheck --enable=all x.c
    Checking x.c ...
    x.c:7:11: style: The scope of the variable 'i' can be reduced. [variableScope]
    myint i;
    ^
    x.c:8:9: style: The scope of the variable 'n' can be reduced. [variableScope]
    int n;
    ^

    I would expect the same message for variables 'en' and 'e' as well.
    Does anyone else consider this a bug?

    Regards,
    Jens

     
  • Daniel Marjamäki

    Thanks! I agree. I created a ticket: https://trac.cppcheck.net/ticket/10365

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.