Use explicit types instead of auto unless the type is obvious from the right-hand side of the assignment or impossible to spell. The LLVM coding standards recommend avoiding auto in most cases to improve code readability and maintainability.
When to use auto:
auto *CI = dyn_cast<CallInst>(I)for (auto it = container.begin(); ...)auto result = make_unique<MyClass>()When to avoid auto:
StringRef name = getValue() instead of auto name = getValue()unsigned count = getCount() instead of auto count = getCount()MLIRContext *context = builder.getContext() instead of auto context = builder.getContext()Example:
// Bad - type not obvious
auto name = getValue();
auto count = getElementCount();
auto context = builder.getContext();
// Good - explicit types improve readability
StringRef name = getValue();
unsigned count = getElementCount();
MLIRContext *context = builder.getContext();
// Good - auto appropriate here
auto *CI = dyn_cast<CallInst>(instruction);
for (auto it = container.begin(); it != container.end(); ++it) { ... }
This practice ensures code remains readable without IDE assistance and makes type information immediately available to reviewers and maintainers.
Enter the URL of a public GitHub repository