[PATCH] D52892: [Clang-tidy: readability] readability check to convert numerical constants to std::numeric_limits
JonasToth added a comment. Hi IdrissRio, thanks for working on this! I have one question: Why are variables _not_ considered in the check but only constants? IMHO it would make sense to transform these as well. I dont have time for long review today anymore, I will continue tomorrow. Comment at: clang-tidy/readability/NumericalCostantsToMaxIntCheck.cpp:22 +namespace { +class NumericalConstCheckPPCallbacks : public PPCallbacks { +public: Unfortunatly this is duplicated effort. Please take a look at `cppcoreguidelines/ProBoundsConstantArrayIndexCheck.cpp` and `utils/IncludeInserter.{h,cpp}` to add new includes from clang-tidy Comment at: clang-tidy/readability/NumericalCostantsToMaxIntCheck.cpp:90 + const auto *Lit = Result.Nodes.getNodeAs("Lit"); + if (Decl == nullptr || Lit == nullptr) +return; Is failure here expected? The matcher should match both nodes simultanously, if so please use an `assert` instead to detect unexpected failures Comment at: docs/clang-tidy/checks/readability-numerical-costants-to-max-int.rst:16 + +unsigned const long int x = -1; + please indent that code, otherwise its is rendered incorrect Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D52892 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D52892: [Clang-tidy: readability] readability check to convert numerical constants to std::numeric_limits
IdrissRio updated this revision to Diff 168321. IdrissRio added a comment. Herald added a subscriber: jfb. Sorry i have forgot to add the source code. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D52892 Files: clang-tidy/readability/CMakeLists.txt clang-tidy/readability/NumericalCostantsToMaxIntCheck.cpp clang-tidy/readability/NumericalCostantsToMaxIntCheck.h clang-tidy/readability/ReadabilityTidyModule.cpp docs/ReleaseNotes.rst docs/clang-tidy/checks/list.rst docs/clang-tidy/checks/readability-numerical-costants-to-max-int.rst test/clang-tidy/readability-numerical-costants-to-max-int.cpp Index: test/clang-tidy/readability-numerical-costants-to-max-int.cpp === --- /dev/null +++ test/clang-tidy/readability-numerical-costants-to-max-int.cpp @@ -0,0 +1,76 @@ +// RUN: %check_clang_tidy %s readability-numerical-costants-to-max-int %t +unsigned const int Uval1 = -1; +// CHECK-MESSAGES: :[[@LINE-2]]:1: warning: Add '#include' to use std::numeric_limits::max() function [readability-numerical-costants-to-max-int] +// CHECK-MESSAGES: :[[@LINE-2]]:29: warning: use 'std::numeric_limits::max()' instead of '-1' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: #include +// CHECK-FIXES: unsigned const int Uval1 = std::numeric_limits::max(); + + +unsigned const long Uval2 = -1; +// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: use 'std::numeric_limits::max()' instead of '-1' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const long Uval2 = std::numeric_limits::max(); + +unsigned const long int Uval3 = -1; +// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use 'std::numeric_limits::max()' instead of '-1' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const long int Uval3 = std::numeric_limits::max(); + +unsigned const long long int Uval4 = -1; +// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: use 'std::numeric_limits::max()' instead of '-1' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const long long int Uval4 = std::numeric_limits::max(); + +unsigned const short int Uval5 = -1; +// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use 'std::numeric_limits::max()' instead of '-1' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const short int Uval5 = std::numeric_limits::max(); + +unsigned const Uval6 = -1; +// CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use 'std::numeric_limits::max()' instead of '-1' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const Uval6 = std::numeric_limits::max(); + +unsigned const char Uval7 = -1; +// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: use 'std::numeric_limits::max()' instead of '-1' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const char Uval7 = std::numeric_limits::max(); + + +//if not constants do nothing +unsigned int Uval8 = -1; +unsigned long Uval9 = -1; +unsigned long int Uval10 = -1; +unsigned long long int Uval11 = -1; +unsigned short int Uval12 = -1; +unsigned Uval13 = -1; +unsigned char Uval14 = -1; + + +unsigned const long Uval15 = ~0; +// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: use 'std::numeric_limits::max()' instead of '~0' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const long Uval15 = std::numeric_limits::max(); + +unsigned const long int Uval16 = ~0; +// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: use 'std::numeric_limits::max()' instead of '~0' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const long int Uval16 = std::numeric_limits::max(); + +unsigned const long long int Uval17 = ~0; +// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: use 'std::numeric_limits::max()' instead of '~0' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const long long int Uval17 = std::numeric_limits::max(); + +unsigned const short int Uval18 = ~0; +// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: use 'std::numeric_limits::max()' instead of '~0' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const short int Uval18 = std::numeric_limits::max(); + +unsigned const Uval19 = ~0; +// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: use 'std::numeric_limits::max()' instead of '~0' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const Uval19 = std::numeric_limits::max(); + +unsigned const char Uval20 = ~0; +// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: use 'std::numeric_limits::max()' instead of '~0' for unsigned constant int [readability-numerical-costants-to-max-int] +// CHECK-FIXES: unsigned const char Uval20 = std::numeric_limits::max(); + + +//if not constants do
[PATCH] D52892: [Clang-tidy: readability] readability check to convert numerical constants to std::numeric_limits
lebedev.ri added a comment. The check itself isn't in the diff. Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D52892 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D52892: [Clang-tidy: readability] readability check to convert numerical constants to std::numeric_limits
IdrissRio created this revision. IdrissRio added reviewers: JonasToth, aaron.ballman, alexfh. Herald added subscribers: cfe-commits, mgorny. Hello, i want to propose this check suggested by @EugeneZelenko. I have found it in the beginner tag of llvm-bugzilla repository. This check looks for numerical unsigned constants that are equal to -1 or ~0 and substitutes them with std::numeric_limits::max(). It includes the library if is not found. Example: unsigned const int x = -1; becomes unsigned const int x = std::numeric_limits::max(); Repository: rCTE Clang Tools Extra https://reviews.llvm.org/D52892 Files: clang-tidy/readability/CMakeLists.txt clang-tidy/readability/ReadabilityTidyModule.cpp docs/ReleaseNotes.rst docs/clang-tidy/checks/list.rst Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -9,8 +9,8 @@ abseil-no-internal-dependencies abseil-no-namespace abseil-redundant-strcat-calls - abseil-string-find-startswith abseil-str-cat-append + abseil-string-find-startswith android-cloexec-accept android-cloexec-accept4 android-cloexec-creat @@ -216,6 +216,7 @@ performance-unnecessary-copy-initialization performance-unnecessary-value-param portability-simd-intrinsics + readability-NumericalCostantsToMaxInt readability-avoid-const-params-in-decls readability-braces-around-statements readability-container-size-empty Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -57,6 +57,12 @@ Improvements to clang-tidy -- +- New :doc:`readability-numerical-costants-to-max-int + ` check. + + Checks for numerical unsigned constants that are equal to -1 or ~0 + and substitutes them with std::numeric_limits::max(). + - New :doc:`abseil-duration-division ` check. Index: clang-tidy/readability/ReadabilityTidyModule.cpp === --- clang-tidy/readability/ReadabilityTidyModule.cpp +++ clang-tidy/readability/ReadabilityTidyModule.cpp @@ -25,6 +25,7 @@ #include "MisplacedArrayIndexCheck.h" #include "NamedParameterCheck.h" #include "NonConstParameterCheck.h" +#include "NumericalCostantsToMaxIntCheck.h" #include "RedundantControlFlowCheck.h" #include "RedundantDeclarationCheck.h" #include "RedundantFunctionPtrDereferenceCheck.h" @@ -72,6 +73,8 @@ "readability-misleading-indentation"); CheckFactories.registerCheck( "readability-misplaced-array-index"); +CheckFactories.registerCheck( +"readability-numerical-costants-to-max-int"); CheckFactories.registerCheck( "readability-redundant-function-ptr-dereference"); CheckFactories.registerCheck( Index: clang-tidy/readability/CMakeLists.txt === --- clang-tidy/readability/CMakeLists.txt +++ clang-tidy/readability/CMakeLists.txt @@ -17,6 +17,7 @@ NamedParameterCheck.cpp NamespaceCommentCheck.cpp NonConstParameterCheck.cpp + NumericalCostantsToMaxIntCheck.cpp ReadabilityTidyModule.cpp RedundantControlFlowCheck.cpp RedundantDeclarationCheck.cpp Index: docs/clang-tidy/checks/list.rst === --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -9,8 +9,8 @@ abseil-no-internal-dependencies abseil-no-namespace abseil-redundant-strcat-calls - abseil-string-find-startswith abseil-str-cat-append + abseil-string-find-startswith android-cloexec-accept android-cloexec-accept4 android-cloexec-creat @@ -216,6 +216,7 @@ performance-unnecessary-copy-initialization performance-unnecessary-value-param portability-simd-intrinsics + readability-NumericalCostantsToMaxInt readability-avoid-const-params-in-decls readability-braces-around-statements readability-container-size-empty Index: docs/ReleaseNotes.rst === --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -57,6 +57,12 @@ Improvements to clang-tidy -- +- New :doc:`readability-numerical-costants-to-max-int + ` check. + + Checks for numerical unsigned constants that are equal to -1 or ~0 + and substitutes them with std::numeric_limits::max(). + - New :doc:`abseil-duration-division ` check. Index: clang-tidy/readability/ReadabilityTidyModule.cpp === --- clang-tidy/readability/ReadabilityTidyModule.cpp +++ clang-tidy/readability/ReadabilityTidyModule.cpp @@ -25,6 +25,7 @@ #include "MisplacedArrayIndexCheck.h" #include "NamedParameterCheck.h" #include "NonConstParameterCheck.h" +#include