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

本小妞迷上赌 提交于 2019-12-12 10:37:50

问题


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?


回答1:


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.




回答2:


enums have data type as unsigned integers.

Update: Seems like it is implementation defined: Are C++ enums signed or unsigned?



来源:https://stackoverflow.com/questions/4872566/cppcheck-thinks-i-have-redundant-code-found-a-statement-that-begins-with-numer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!