[PATCH] D52892: [Clang-tidy: readability] readability check to convert numerical constants to std::numeric_limits

Idriss Riouak via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 4 09:44:19 PDT 2018


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<type>::max().

It includes the library <limits> if is not found.

Example:

unsigned const int x = -1;

becomes

unsigned const int x = std::numeric_limits<unsigned int>::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
+  <clang-tidy/checks/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<type>::max().
+
 - New :doc:`abseil-duration-division
   <clang-tidy/checks/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<MisplacedArrayIndexCheck>(
         "readability-misplaced-array-index");
+    CheckFactories.registerCheck<NumericalCostantsToMaxIntCheck>(
+        "readability-numerical-costants-to-max-int");
     CheckFactories.registerCheck<RedundantFunctionPtrDereferenceCheck>(
         "readability-redundant-function-ptr-dereference");
     CheckFactories.registerCheck<RedundantMemberInitCheck>(
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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52892.168316.patch
Type: text/x-patch
Size: 2813 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181004/c05e0b21/attachment.bin>


More information about the cfe-commits mailing list