Skip to content

Commit 288739c

Browse files
Merge pull request #2844 from dmitrykobets/dmitrykobets-patch-2
Create c26819.md
2 parents cdaed30 + 50c3284 commit 288739c

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

docs/code-quality/c26819.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
---
2+
title: C26819
3+
description: "Reference for Microsoft C++ Code Analysis warning C26819 in Visual Studio."
4+
ms.date: 04/22/2020
5+
ms.topic: "reference"
6+
f1_keywords: ["C26819"]
7+
helpviewer_keywords: ["C26819"]
8+
---
9+
# C26819
10+
11+
> Unannotated fallthrough between switch labels (es.78).
12+
13+
## Remarks
14+
15+
This check covers implicit fallthrough in switch statements. Implicit fallthrough is when control flow transfers from one switch case directly into a following switch case without the use of the `[[fallthrough]];` statement. This warning is raised when an implicit fallthrough is detected in a switch case containing at least one statement.
16+
17+
For more information, see [ES.78: Don't rely on implicit fallthrough in `switch` statements](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-break) in the C++ Core Guidelines.
18+
19+
## Example
20+
21+
In this sample, implicit fallthrough occurs from a non-empty switch case into a following case.
22+
23+
```cpp
24+
void fn1();
25+
void fn2();
26+
27+
void foo(int a)
28+
{
29+
switch (a)
30+
{
31+
case 0: // implicit fallthrough from case 0 to case 1 is OK because case 0 is empty
32+
case 1:
33+
fn1(); // implicit fallthrough from case 1 into case 2
34+
case 2:
35+
fn2();
36+
break;
37+
default:
38+
break;
39+
}
40+
}
41+
```
42+
43+
## Solution
44+
45+
To fix this issue, insert a `[[fallthrough]];` statement where the fallthrough occurs.
46+
47+
```cpp
48+
void fn1();
49+
void fn2();
50+
51+
void foo(int a)
52+
{
53+
switch (a)
54+
{
55+
case 0:
56+
case 1:
57+
fn1();
58+
[[fallthrough]]; // fallthrough is explicit
59+
case 2:
60+
fn2();
61+
break;
62+
default:
63+
break;
64+
}
65+
}
66+
```
67+
68+
Another way to fix the issue is to remove the implicit fallthrough.
69+
70+
```cpp
71+
void fn1();
72+
void fn2();
73+
74+
void foo(int a)
75+
{
76+
switch (a)
77+
{
78+
case 0:
79+
case 1:
80+
fn1();
81+
break; // case 1 no longer falls through into case 2
82+
case 2:
83+
fn2();
84+
break;
85+
default:
86+
break;
87+
}
88+
}
89+
```

0 commit comments

Comments
 (0)