Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cppcheck thinks I have "Redundant code: Found a statement that begins with numeric constant"

Cppcheck (version 1.46.1) gives the following warning for an enum like this one:

enum DATABASE_TYPE
{
    DATABASE_TYPE_UNKNOWN = -1, // <- line of warning
    DATABASE_TYPE_ORACLE,
    DATABASE_TYPE_MSACCESS
};

Redundant code: Found a statement that begins with numeric constant

I don't think, that it's redundant. It's quite important to be able to do things like that.

Is this an error of cppcheck or am I not seeing something?

Update

I managed to boil it down to a minimal example. This was complicated by cppcheck having 2 (further) bugs which made it look like my reductions had no effect.
There are 5 files: a.h, a.cpp, b.h, b.cpp and inc.h with the following content.
VC9 compiles it without warnings (warning level 4).

// a.h
#pragma once
#include "inc.h"

// a.cpp
#include "a.h"
#include "b.h"

int main()
{
    return 0;
}


// b.h
#pragma once
#include "inc.h"

// b.cpp
#include "b.h"

//inc.h
#pragma once

enum MY_ENUM_TYPE
{
    INVALID_VALUE = -1,
    FIRST_VALUE,
    SECOND_VALUE
};

So by now I'm pretty confident that it's a bug of cppcheck. Any diverging opinions?

like image 829
foraidt Avatar asked Feb 02 '11 09:02

foraidt


1 Answers

My guess is, either:

A) invalid somehow is declared or defined elsewhere.

B) the enum is defined in a header included twice (without header guards). Because you get the same error for this code:

enum SomeEnumType
{
    invalid = -1,
    first,
    second,
};

enum SomeEnumType
{
    invalid = -1, // <- line of warning
    first,
    second,
};

Does your code compile with GCC?


UPDATE:

Yes, this seems like a cppcheck bug - #pragma once is not working. If you replace it with #ifndef A_H / #define A_H / #endif header wrapping, cppcheck does not complain anymore.

This also seems like a recognized problem.

like image 165
Johan Kotlinski Avatar answered Nov 01 '22 05:11

Johan Kotlinski