本文介绍了cppcheck认为我有“冗余代码:找到以数字常数开头的语句”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下警告对于像这样的枚举:

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
};



我不认为这是多余的。这是非常重要的能够做这样的事情。

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

这是cppcheck的错误还是没有看到什么?

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

我设法将其简化为一个最小的例子。这是复杂的cppcheck有2(进一步)的错误,使它看起来像我的减少没有效果。

有5个文件: a.cpp bh b.cpp inc.h 包含以下内容。

VC9编译没有警告(警告级别4)。

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
};

所以现在我很自信,这是一个cppcheck的错误。任何分歧的意见?

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

推荐答案

我的猜测是:

A)无效以某种方式在其他地方声明或定义。

A) invalid somehow is declared or defined elsewhere.

B)枚举在包含两次的标题中定义没有后卫)。因为您的代码有相同的错误:

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,
};

您的代码是否使用GCC编译?

Does your code compile with GCC?

更新:

是的,这似乎是一个cppcheck错误 - #pragma once 不工作。如果将其替换为 #ifndef A_H / #define A_H / #endif 标题包装,cppcheck不再抱怨了。

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.

这也似乎是一个。

这篇关于cppcheck认为我有“冗余代码:找到以数字常数开头的语句”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 22:05