Add braces for CARBON_KIND uses that lack them (#5882)

Also document why these are expected on `CARBON_KIND`. In
`kind_switch_test.cpp`, drop the `str` variable.

My recollection of the original discussion of `CARBON_KIND` is that it
should always have braces due to the risk of confusion for statement
interpretation, similar to a typical `if`/`else` but more subtle due to
the macro.

For example:

```
      case CARBON_KIND(int n):
        str << "int = " << n;
        return str.TakeStr();
```

is equivalent to:

```
      case CARBON_KIND(int n): {
          str << "int = " << n;
        }
        return str.TakeStr();
```

This happens to work in context because `str` isn't scoped, but a
trivial refactoring to move `RawStringOstream str;` the first statement
of the `case` would probably have non-obvious results. For example:

```
      case CARBON_KIND(int n):
        RawStringOstream str; // Valid name shadowing, destructed without use.
        str << "int = " << n; // Name lookup error on `n`.
        return str.TakeStr();
```
This commit is contained in:
Jon Ross-Perkins
2025-07-30 18:56:39 +00:00
committed by GitHub
parent 4c0979fc10
commit 800e8fd55a
6 changed files with 83 additions and 49 deletions
+2 -1
View File
@@ -92,8 +92,9 @@ static auto DiagnoseFinalImplNotInSameFileAsRootSelfTypeOrInterface(
break;
}
case CARBON_KIND(Step::Done _):
case CARBON_KIND(Step::Done _): {
CARBON_FATAL("self type is empty?");
}
default:
break;