aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergio Martins <smartins@kde.org>2021-07-27 22:49:34 +0100
committerSergio Martins <smartins@kde.org>2021-07-27 22:49:34 +0100
commit02a5e362793993c47b8d6af96fcdfc5a916b4c1c (patch)
treed9b5cb08f828eb23c0cb35decb9da0614a229380
parent215634c156221aa03d0b515f2266752c5651c25c (diff)
unexpected-flag-enumerator-value: fix flag detection
A non-flag enum doesn't need to have all of its enumerator consecutive. See example added in the test, that shouldn't warn
-rw-r--r--src/checks/manuallevel/unexpected-flag-enumerator-value.cpp11
-rw-r--r--tests/unexpected-flag-enumerator-value/main.cpp11
2 files changed, 17 insertions, 5 deletions
diff --git a/src/checks/manuallevel/unexpected-flag-enumerator-value.cpp b/src/checks/manuallevel/unexpected-flag-enumerator-value.cpp
index d48be74e..b953f622 100644
--- a/src/checks/manuallevel/unexpected-flag-enumerator-value.cpp
+++ b/src/checks/manuallevel/unexpected-flag-enumerator-value.cpp
@@ -28,6 +28,8 @@
#include <clang/AST/AST.h>
+#include <algorithm>
+
using namespace clang;
using namespace std;
@@ -75,12 +77,13 @@ static uint64_t getIntegerValue(EnumConstantDecl* e)
static bool hasConsecutiveValues(const SmallVector<EnumConstantDecl*, 16>& enumerators)
{
auto val = getIntegerValue(enumerators.front());
- bool consecutive = true;
- for (size_t i = 1; i < enumerators.size(); ++i) {
+ const size_t until = std::min<size_t>(4, enumerators.size());
+ for (size_t i = 1; i < until; ++i) {
val++;
- consecutive = getIntegerValue(enumerators[i]) == val;
+ if (getIntegerValue(enumerators[i]) != val)
+ return false;
}
- return consecutive;
+ return true;
}
static IsFlagEnumResult isFlagEnum(const SmallVector<EnumConstantDecl*, 16>& enumerators)
diff --git a/tests/unexpected-flag-enumerator-value/main.cpp b/tests/unexpected-flag-enumerator-value/main.cpp
index bb2d19f4..3d7884df 100644
--- a/tests/unexpected-flag-enumerator-value/main.cpp
+++ b/tests/unexpected-flag-enumerator-value/main.cpp
@@ -39,7 +39,7 @@ enum AlignmentFlag {
};
// No Warning here, all consecutive values
-enum {
+enum Consecutive {
RED = 1,
GREEN = 2,
BLUE = 3,
@@ -72,3 +72,12 @@ enum class WithZero {
Four = 4,
Eight = 8,
};
+
+enum class AlsoConsecutive {
+ RED = 1,
+ GREEN = 2,
+ BLUE = 3,
+ BLACK = 4,
+ WHITE = 5,
+ LAST = WHITE
+};