[clang-tools-extra] [clang-tidy] [bugprone-implicit-widening-of-multiplication-result-int] Prefer language literal suffix than static_cast (PR #198258)

Daniil Dudkin via cfe-commits cfe-commits at lists.llvm.org
Sun May 24 13:35:16 PDT 2026


================
@@ -24,6 +25,74 @@ AST_MATCHER(ImplicitCastExpr, isPartOfExplicitCast) {
 AST_MATCHER(Expr, containsErrors) { return Node.containsErrors(); }
 } // namespace
 
+static std::optional<StringRef> getLiteralSuffix(QualType Ty,
+                                                 const ASTContext &Context) {
+  if (!Ty->isIntegerType() || Ty->isBitIntType())
+    return std::nullopt;
+
+  const LangOptions &LangOpts = Context.getLangOpts();
+  const QualType CanonTy = Ty.getCanonicalType();
+
+  if (Context.getIntWidth(CanonTy) <= Context.getIntWidth(Context.IntTy))
+    return std::nullopt;
+
+  if (Context.hasSameType(CanonTy, Context.LongTy))
+    return "l";
+  if (Context.hasSameType(CanonTy, Context.UnsignedLongTy))
+    return "ul";
+
+  const bool HasLongLongLiteralSuffix = LangOpts.CPlusPlus11 || LangOpts.C99;
+  if (!HasLongLongLiteralSuffix)
+    return std::nullopt;
+
+  if (Context.hasSameType(CanonTy, Context.LongLongTy))
+    return "ll";
+  if (Context.hasSameType(CanonTy, Context.UnsignedLongLongTy))
+    return "ull";
+
+  return std::nullopt;
----------------
unterumarmung wrote:

Should we add support for `size_t` literals? maybe in next PR..

https://github.com/llvm/llvm-project/pull/198258


More information about the cfe-commits mailing list