Skip to content

Commit 2ca6ce5

Browse files
Merge pull request #4900 from michaelwoerister/update-c33010-and-c33011
Update code-quality/c33010.md and /code-quality/c33011.md
2 parents 9578a7a + cd62c85 commit 2ca6ce5

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

docs/code-quality/c33010.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ helpviewer_keywords: ["C33010"]
99
---
1010
# Warning C33010
1111

12-
> Unchecked lower bound for enum *enum_name* used as index.
12+
> Unchecked lower bound for enum 'enum' used as index.
1313
1414
This warning is triggered if an enum is both used as an index into an array and isn't checked on the lower bound.
1515

@@ -71,6 +71,31 @@ void foo(Index idx, PFN(&functions)[5])
7171
}
7272
```
7373

74+
Alternatively, the issue can be fixed by choosing an underlying type for `Index` that is unsigned. Because an unsigned value is always positive, it is sufficient to only check the upper bound.
75+
76+
```cpp
77+
typedef void (*PFN)();
78+
79+
enum class Index : unsigned int
80+
{
81+
Zero,
82+
One,
83+
Two,
84+
Three,
85+
Max
86+
};
87+
88+
void foo(Index idx, PFN(&functions)[5])
89+
{
90+
if (idx > Index::Max)
91+
return;
92+
93+
auto pfn = functions[static_cast<unsigned int>(idx)];
94+
if (pfn != nullptr)
95+
(*pfn)();
96+
}
97+
```
98+
7499
## See also
75100
76101
[C33011](./c33011.md)

docs/code-quality/c33011.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: C33011 warning for enums
44
author: hwisungi
55
ms.author: hwisungi
66
ms.date: 06/20/2020
7-
f1_keywords: ["C33011", "UNCHECKED_UPPER_BOUND_FOR_ENUMINDEX"]
7+
f1_keywords: ["C33011", "UNCHECKED_UPPER_BOUND_FOR_ENUMINDEX", "__WARNING_UNCHECKED_UPPER_BOUND_FOR_ENUMINDEX"]
88
helpviewer_keywords: ["C33011"]
99
---
1010
# Warning C33011

0 commit comments

Comments
 (0)