google / google/styleguide

google-c-style.el improperly indents switch-case fallthrough

Open
#427 0 comments 0 reactions 0 assignees View on GitHub
lang:cpp
Dominant language
HTML
Stars
39.6k
Forks
12.9k
Avg merge
42m
Merged PRs (30d)
15

Description

The `google-c-style.el` loaded style suggests the following indentation in my C++ code:
```c++
case (MG_CLEARBND2R):
case (MG_CLEARBND2B):
x = 2;
break;
case (MG_SENDBND0):
...
```
Note that the `break;` statement should have a +2 indent relative to `case` = be at the same indentation level as the previous line:
>
```c++
case (MG_CLEARBND2R):
case (MG_CLEARBND2B):
x = 2;
break;
case (MG_SENDBND0):
...
```
According to [Loops and Switch Statements](https://google.github.io/styleguide/cppguide.html#Loops_and_Switch_Statements):
> case blocks in switch statements can have curly braces or not, depending on your preference. If you do include curly braces they should be placed as shown below.

The example for the multiple `case` label block illustrates this:
```c++
switch (x) {
case 41: // No annotation needed here.
case 43:
if (dont_be_picky) {
// Use this instead of or along with annotations in comments.
ABSL_FALLTHROUGH_INTENDED;
} else {
CloseButNoCigar();
break;
}
case 42:
DoSomethingSpecial();
ABSL_FALLTHROUGH_INTENDED;
default:
DoSomethingGeneric();
break;
}
```

Adding the (supposedly optional) braces fixes the indentation behavior:
```c++
case (MG_CLEARBND2R):
case (MG_CLEARBND2B): {
x = 2;
break;
}
case (MG_SENDBND0):
...
```
This problem only occurs for case blocks preceded by a case label without intervening code.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.