https://github.com/Aaravkhanal created https://github.com/llvm/llvm-project/pull/246962
The `readability-uppercase-literal-suffix` check can report a false positive for a consteval user-defined integer literal in a default member initializer when the containing class has a user-provided constructor. The literal's `IntegerLiteral` is also visited as part of the synthesized `CXXDefaultInitExpr` used in the constructor initializer list. In that path, the matcher can mistake the trailing `z` in the UDL suffix for an integer suffix. Skip that synthesized UDL path while retaining diagnostics for ordinary integer literals. Add regression coverage for the false positive, a nearby genuine `0z` suffix warning/fix-it, and a defaulted constructor. Add a clang-tidy release note. Validation: `git diff --check` passes. The issue reproduces with clang-tidy 23.1.4, but the checkout has no built clang-tidy test harness, so `%check_clang_tidy` was not run. Fixes #246905 Assisted-by: OpenAI Codex >From 20dbf62c6f5d778a970007991c22ab8dc9c140a6 Mon Sep 17 00:00:00 2001 From: Aaravkhanal <[email protected]> Date: Sun, 29 Nov 2026 16:50:32 +0530 Subject: [PATCH] Fix UDL suffix false positive in default member initializer Assisted-by: OpenAI Codex --- .../UppercaseLiteralSuffixCheck.cpp | 35 +++++++++++++++++++ clang-tools-extra/docs/ReleaseNotes.md | 4 +++ .../uppercase-literal-suffix-integer.cpp | 17 +++++++++ 3 files changed, 56 insertions(+) diff --git a/clang-tools-extra/clang-tidy/readability/UppercaseLiteralSuffixCheck.cpp b/clang-tools-extra/clang-tidy/readability/UppercaseLiteralSuffixCheck.cpp index 28410f23a812e0..aeb493e0ed05cd 100644 --- a/clang-tools-extra/clang-tidy/readability/UppercaseLiteralSuffixCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/UppercaseLiteralSuffixCheck.cpp @@ -10,6 +10,7 @@ #include "../utils/ASTUtils.h" #include "../utils/OptionsUtils.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/ParentMapContext.h" #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Lex/Lexer.h" #include <optional> @@ -203,6 +204,40 @@ void UppercaseLiteralSuffixCheck::check( const auto *const Literal = Result.Nodes.getNodeAs<Expr>("expr"); const bool IsInteger = isa<IntegerLiteral>(Literal); + // A consteval user-defined literal in a default member initializer can be + // evaluated while building a user-provided constructor's initializer list. + // In that context, the literal's suffix may be mistaken for an integer + // suffix. Do not diagnose literals that are part of such an initializer. + if (IsInteger) { + const auto &Parents = Result.Context->getParentMapContext(); + for (const DynTypedNode &Parent : Parents.getParents(*Literal)) { + const auto *UDL = Parent.get<UserDefinedLiteral>(); + if (!UDL) + continue; + + const DynTypedNodeList UDLParents = Parents.getParents(*UDL); + if (UDLParents.size() != 1) + continue; + const auto *DefaultInit = dyn_cast_or_null<CXXDefaultInitExpr>( + UDLParents.begin()->get<Stmt>()); + if (!DefaultInit) + continue; + + const auto *Field = DefaultInit->getField(); + if (!Field) + continue; + + const auto *Record = dyn_cast<CXXRecordDecl>(Field->getParent()); + if (!Record) + continue; + + if (llvm::any_of(Record->ctors(), [](const CXXConstructorDecl *Ctor) { + return Ctor->isUserProvided(); + })) + return; + } + } + // We won't *always* want to diagnose. // We might have a suffix that is already uppercase. if (auto Details = shouldReplaceLiteralSuffix( diff --git a/clang-tools-extra/docs/ReleaseNotes.md b/clang-tools-extra/docs/ReleaseNotes.md index 15c76ccab6b0d7..f82d4c69b0656a 100644 --- a/clang-tools-extra/docs/ReleaseNotes.md +++ b/clang-tools-extra/docs/ReleaseNotes.md @@ -137,6 +137,10 @@ infrastructure are described first, followed by tool-specific sections. ### Improvements to clang-tidy +- Fixed a false positive in `readability-uppercase-literal-suffix` for + user-defined literals in default member initializers of classes with + user-provided constructors. + - Improved {program}`check_clang_tidy.py` by adding support of `-std=cXX-or-earlier` values, mirroring the existing `-std=cXX-or-later`. New construct expands to the given standard and every earlier one. diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/uppercase-literal-suffix-integer.cpp b/clang-tools-extra/test/clang-tidy/checkers/readability/uppercase-literal-suffix-integer.cpp index 88d4303229a5ae..0aa1175c2e173c 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/readability/uppercase-literal-suffix-integer.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/readability/uppercase-literal-suffix-integer.cpp @@ -238,3 +238,20 @@ void test_non_type_template_parameter() { int x = Vector<10>::kCapacity; int f = Foo<10>::kFoo; } + +consteval int operator""_goz(unsigned long long Value) { + return static_cast<int>(Value); +} + +struct UserProvidedConstructor { + UserProvidedConstructor() {} + int Value = 0_goz; + int OrdinaryZ = 0z; + // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: integer literal has suffix 'z', which is not uppercase + // CHECK-FIXES: int OrdinaryZ = 0Z; +}; + +struct DefaultedConstructor { + DefaultedConstructor() = default; + int Value = 0_goz; +}; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
