[clang-tools-extra] [clang-tidy][docs] improve documentation on cppcoreguidelines-narrowing-conversions (#111510) (PR #118209)

Jonas Toth via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 3 10:26:43 PST 2024


https://github.com/JonasToth updated https://github.com/llvm/llvm-project/pull/118209

>From 4340df440863de154ed305dc487d23ef46c9277a Mon Sep 17 00:00:00 2001
From: Jonas Toth <development at jonas-toth.eu>
Date: Sun, 1 Dec 2024 12:02:13 +0100
Subject: [PATCH 1/2] [clang-tidy][docs] improve documentation on
 cppcoreguidelines-narrowing-conversions (#111510)

---
 .../narrowing-conversions.rst                 | 33 +++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
index 04260e75aa558f..643266193f15ff 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
@@ -27,6 +27,39 @@ This check will flag:
  - All applications of binary operators with a narrowing conversions.
    For example: ``int i; i+= 0.1;``.
 
+Arithmetic with smaller integer types than ``int`` trigger implicit conversions,
+as explained under `"Integral Promotion" on cppreference.com
+<https://en.cppreference.com/w/cpp/language/implicit_conversion>`_.
+This check diagnoses more instances of narrowing than the compiler warning
+``-Wconversion`` does. The example below demonstrates this behavior.
+
+.. code-block:: c++
+
+   // The following function definition demonstrates usage of arithmetic with
+   // integer types smaller than `int` and how the narrowing conversion happens
+   // implicitly.
+   void computation(short argument1, short argument2) {
+     // Arithmetic written by humans:
+     short result = argument1 + argument2;
+     // Arithmetic actually performed by C++:
+     short result = static_cast<short>(static_cast<int>(argument1) + static_cast<int>(argument2));
+   }
+
+   void recommended_resolution(short argument1, short argument2) {
+     short result = argument1 + argument2;
+     //           ^ warning: narrowing conversion from 'int' to signed type 'short' is implementation-defined
+
+     // The recommended way to resolve this issue with the GSL is one of two ways.
+     // Either by a cast that throws if a loss of precision would occur.
+     short result = gsl::narrow<short>(argument1 + argument2);
+     // Or it can be resolved without checking the result.
+     short result = gsl::narrow_cast<short>(argument1 + argument2);
+
+     // A classical `static_cast` will silence the warning as well if the GSL
+     // is not available.
+     short result = static_cast<short>(argument1 + argument2);
+   }
+
 
 Options
 -------

>From 5f49a956ab463ef2614a535b4f397e9c0c94caa6 Mon Sep 17 00:00:00 2001
From: Jonas Toth <development at jonas-toth.eu>
Date: Tue, 3 Dec 2024 19:26:35 +0100
Subject: [PATCH 2/2] Update
 clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst

Co-authored-by: EugeneZelenko <eugene.zelenko at gmail.com>
---
 .../checks/cppcoreguidelines/narrowing-conversions.rst          | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
index 643266193f15ff..6b0c3187b40874 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
@@ -31,7 +31,7 @@ Arithmetic with smaller integer types than ``int`` trigger implicit conversions,
 as explained under `"Integral Promotion" on cppreference.com
 <https://en.cppreference.com/w/cpp/language/implicit_conversion>`_.
 This check diagnoses more instances of narrowing than the compiler warning
-``-Wconversion`` does. The example below demonstrates this behavior.
+`-Wconversion` does. The example below demonstrates this behavior.
 
 .. code-block:: c++
 



More information about the cfe-commits mailing list