@@ -16,6 +16,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
16
16
- [ General naming rules] ( #general-naming-rules )
17
17
- [ File names] ( #file-names )
18
18
- [ Syntax and formatting] ( #syntax-and-formatting )
19
+ - [ Naming variable types and the use of ` auto ` ] ( #naming-variable-types-and-the-use-of-auto )
19
20
- [ Copyable and movable types] ( #copyable-and-movable-types )
20
21
- [ Static and global variables] ( #static-and-global-variables )
21
22
- [ Foundational libraries and data types] ( #foundational-libraries-and-data-types )
@@ -197,6 +198,22 @@ these.
197
198
`protected`. This is motivated by the
198
199
`misc-non-private-member-variables-in-classes` tidy check.
199
200
201
+ ### Naming variable types and the use of `auto`
202
+
203
+ We generally use `auto` for most local variables when a type can be inferred,
204
+ except for primitive types such as `bool` and `int`. It is not required to use
205
+ `auto`, and shorter type names such as `SemIR::InstId` are sometimes named even
206
+ though they could be inferred. Naming the type can be helpful in cases where the
207
+ type would be obscure and can not be explained with the variable name. Function
208
+ parameters generally name the type of each parameter, though lambdas may use
209
+ `auto` if it's helpful.
210
+
211
+ When naming variables, we typically suffix `_id` for ID types. When needed, we
212
+ can also resolve ambiguity by referring to the full type name in the variable
213
+ name; for example, if there's a `ClassId`, `InstId`, and `TypeId` for the same
214
+ class entity, we might call these `class_id`, `class_inst_id`, and
215
+ `class_type_id`. Similarly, we might call an `Inst` `class_inst`.
216
+
200
217
### Copyable and movable types
201
218
202
219
- Types should have value semantics and support both move and copy where
0 commit comments