diff --git a/clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/ImplicitWideningOfMultiplicationResultCheck.cpp @@ -34,6 +34,13 @@ return BO->getLHS()->IgnoreParens(); } +static bool hasIntegerLiteralOperators(const Expr* E) +{ + const auto *BO = dyn_cast(E); + return isa(BO->getLHS()->IgnoreParenImpCasts()) && + isa(BO->getRHS()->IgnoreParenImpCasts()); +} + ImplicitWideningOfMultiplicationResultCheck:: ImplicitWideningOfMultiplicationResultCheck(StringRef Name, ClangTidyContext *Context) @@ -89,6 +96,10 @@ if (!LHS) return; + // Widening multiplication on Integer Literals. + if(hasIntegerLiteralOperators(E)) + return; + // Ok, looks like we should diagnose this. diag(E->getBeginLoc(), "performing an implicit widening conversion to type " "%0 of a multiplication performed in type %1") diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -173,6 +173,10 @@ `, so that it does not warn on macros starting with underscore and lowercase letter. +- Improved :doc:`bugprone-implicit-widening-of-multiplication-result + ` + check to ignore false-positives with integer literals. + - Improved :doc:`bugprone-lambda-function-name ` check by adding option `IgnoreMacros` to ignore warnings in macros. diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/implicit-widening-of-multiplication-result-int.cpp @@ -107,10 +107,28 @@ long n14(int a, int b, int c) { return a + b * c; } + long n15(int a, int b, int c) { return a * b + c; } +unsigned long n16() +{ + return (1024u) * 1024; +} + +long n17(int a) { + return a + 1024 * 1024; +} + +long n18(int a) +{ + return (a * 1024); + // CHECK-NOTES-ALL: :[[@LINE-1]]:11: warning: performing an implicit widening conversion to type 'long' of a multiplication performed in type 'int' + // CHECK-NOTES-ALL: :[[@LINE-2]]:11: note: make conversion explicit to silence this warning + // CHECK-NOTES-ALL: :[[@LINE-3]]:11: note: perform multiplication in a wider type +} + #ifdef __cplusplus template T2 template_test(T1 a, T1 b) {