[clang-tools-extra] [clang-tidy] Avoid unsafe `use-default-member-init` fixes (PR #191607)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 14 07:00:41 PDT 2026
================
@@ -301,41 +368,52 @@ void UseDefaultMemberInitCheck::checkDefaultInit(
if (StartLoc.isMacroID() && IgnoreMacros)
return;
- const SourceLocation FieldEnd =
- Lexer::getLocForEndOfToken(Field->getSourceRange().getEnd(), 0,
- *Result.SourceManager, getLangOpts());
- const SourceLocation LParenEnd = Lexer::getLocForEndOfToken(
- Init->getLParenLoc(), 0, *Result.SourceManager, getLangOpts());
- const CharSourceRange InitRange =
- CharSourceRange::getCharRange(LParenEnd, Init->getRParenLoc());
-
- const Expr *InitExpression = Init->getInit();
- const QualType InitType = InitExpression->getType();
-
- const bool ValueInit =
- isa<ImplicitValueInitExpr>(InitExpression) && !isa<ArrayType>(InitType);
- const bool CanAssign =
- UseAssignment && (!ValueInit || !InitType->isEnumeralType());
- const bool NeedsBraces = !CanAssign || isa<ArrayType>(InitType);
-
- auto Diag =
- diag(Field->getLocation(), "use default member initializer for %0")
- << Field;
-
- if (CanAssign)
- Diag << FixItHint::CreateInsertion(FieldEnd, " = ");
- if (NeedsBraces)
- Diag << FixItHint::CreateInsertion(FieldEnd, "{");
-
- if (CanAssign && ValueInit)
- Diag << FixItHint::CreateInsertion(FieldEnd, getValueOfValueInit(InitType));
- else
- Diag << FixItHint::CreateInsertionFromRange(FieldEnd, InitRange);
-
- if (NeedsBraces)
- Diag << FixItHint::CreateInsertion(FieldEnd, "}");
+ {
----------------
vbvictor wrote:
Instead of making another scope, can we do:
```cpp
auto Diag = ...
if (!EmitFixit) {
Diag.Emit();
diag("...", Note)
return;
}
// Fixit logic
```
https://github.com/llvm/llvm-project/pull/191607
More information about the cfe-commits
mailing list