diff --git a/docs/code-quality/c26813.md b/docs/code-quality/c26813.md new file mode 100644 index 00000000000..408b2277c94 --- /dev/null +++ b/docs/code-quality/c26813.md @@ -0,0 +1,48 @@ +--- +description: "Learn more about: C26813" +title: c26813 +keywords: c26813 +ms.date: 02/23/2022 +ms.topic: reference +f1_keywords: ["C26813"] +helpviewer_keywords: ["C26813"] +dev_langs: ["C++"] +--- +# C26813 + +> Warning C26813: Use 'bitwise and' to check if a flag is set + +Most `enum`s with power of two member values are intended to be used with bit flags. As a result, we rarely want to equality compare these flags. We want to extract the bits we are interested in using bitwise operations instead. + +## Example + +```cpp +enum BitWise +{ + A = 1, + B = 2, + C = 4 +}; + +void useEqualsWithBitwiseEnum(BitWise a) +{ + if (a == B) // Warning C26813: Use 'bitwise and' to check if a flag is set + return; +} +``` + +To fix the warning, use bitwise operations: + +```cpp +void useEqualsWithBitwiseEnum(BitWise a) +{ + if (a & B) // Fixed. + return; +} +``` + + +## See also + +[C26827](./c26827.md) +[C26828](./c26828.md) diff --git a/docs/code-quality/c26827.md b/docs/code-quality/c26827.md new file mode 100644 index 00000000000..a9164e817ac --- /dev/null +++ b/docs/code-quality/c26827.md @@ -0,0 +1,54 @@ +--- +description: "Learn more about: C26827" +title: c26827 +keywords: c26814 +ms.date: 02/23/2022 +ms.topic: reference +f1_keywords: ["C26827"] +helpviewer_keywords: ["C26827"] +dev_langs: ["C++"] +--- +# C26827 + +> Warning C26827: Did you forgot to initialize an enum or wanted to use another type? + +Most `enum` types used in bitwise operations are expected to have members with values of powers of two. This warning attempts to find cases where we forgot to give a value to an enum constant explicitly or inadvertently used the wrong enum type. + +## Example + +```cpp +enum class AlmostBitWise +{ + A = 1, + B = 2, + C = 4, + D +}; + +int almostBitwiseEnums(AlmostBitWise a, bool cond) +{ + return (int)a|(int)AlmostBitWise::A; // Warning C26827: Did you forgot to initialize an enum or wanted to use another type? +} +``` + +To fix the warning, initialize the enum constant to the correct value or use the correct enum type in the operation. + +```cpp +enum class AlmostBitWise +{ + A = 1, + B = 2, + C = 4, + D = 8 +}; + +int almostBitwiseEnums(AlmostBitWise a, bool cond) +{ + return (int)a|(int)AlmostBitWise::A; // No warning. +} +``` + +## See also + +[C26813](./c26813.md) +[C26828](./c26828.md) diff --git a/docs/code-quality/c26828.md b/docs/code-quality/c26828.md new file mode 100644 index 00000000000..c469fbd9f95 --- /dev/null +++ b/docs/code-quality/c26828.md @@ -0,0 +1,63 @@ +--- +description: "Learn more about: C26828" +title: c26828 +keywords: c26828 +ms.date: 02/23/2022 +ms.topic: reference +f1_keywords: ["C26828"] +helpviewer_keywords: ["C26828"] +dev_langs: ["C++"] +--- +# C26828 + +> Warning C26828: Different enum types have overlapping values. Did you want to use another enum constant here? + +Most of the times we use a single enum to describe all the bit flags we can use for an option. If we use two different enum types in the same bitwise expression where the enums have overlapping values the chances are good those enums were not designed to be used in the expression. + +## Example + +```cpp + +enum BitWiseA +{ + A = 1, + B = 2, + C = 4 +}; + +enum class BitWiseB +{ + AA = 1, + BB = 2, + CC = 4, + All = 7 +}; + +int overlappingBitwiseEnums(BitWiseA a) +{ + return (int)a|(int)BitWiseB::AA; // Warning C26828: Different enum types have overlapping values. Did you want to use another enum constant here? +} +``` + +To fix the warning make sure `enum`s that are designed to be used together have no overlapping values or make sure all the related options are in a single `enum`. + +```cpp + +enum BitWiseA +{ + A = 1, + B = 2, + C = 4 +}; + +int overlappingBitwiseEnums(BitWiseA a) +{ + return (int)a|(int)BitWiseA::A; // No warning. +} +``` + +## See also + +[C26813](./c26813.md) +[C26827](./c26827.md) + diff --git a/docs/code-quality/toc.yml b/docs/code-quality/toc.yml index d487a2fd3d6..e2ccc86bd1c 100644 --- a/docs/code-quality/toc.yml +++ b/docs/code-quality/toc.yml @@ -579,8 +579,14 @@ items: href: ../code-quality/c26810.md - name: C26811 href: ../code-quality/c26811.md + - name: C26813 + href: ../code-quality/c26813.md - name: C26826 href: ../code-quality/c26826.md + - name: C26827 + href: ../code-quality/c26827.md + - name: C26828 + href: ../code-quality/c26828.md - name: C28020 href: ../code-quality/c28020.md - name: C28021