-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-tidy][docs] improve documentation on cppcoreguidelines-narrowing-conversions (#111510) #118209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang-tidy][docs] improve documentation on cppcoreguidelines-narrowing-conversions (#111510) #118209
Conversation
@llvm/pr-subscribers-clang-tidy @llvm/pr-subscribers-clang-tools-extra Author: Jonas Toth (JonasToth) ChangesThis PR improves the docs for this check to include an example of hidden narrowing conversions from the integer promotion rules in arithmetic. Full diff: https://github.com/llvm/llvm-project/pull/118209.diff 1 Files Affected:
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..f4d1976ad4c688 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,21 @@ This check will flag:
- All applications of binary operators with a narrowing conversions.
For example: ``int i; i+= 0.1;``.
+Note that arithmetic with integer types may perform implicit conversions if the used integer types are smaller than ``int``.
+These rules are documented under `"Integral Promotion" on this cppreference.com <https://en.cppreference.com/w/cpp/language/implicit_conversion>`_
+page. The following example demonstrates this behavior and can be explored with `cppinsights.io <https://cppinsights.io/s/68553908>`_.
+
+.. 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));
+ }
+
Options
-------
|
c662917
to
b10c536
Compare
@PiotrZSL I did assign you as reviewer because I noticed you do many reviews. Please remove yourself if that was not appropriate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Formatting
- Please think about simplifying this.
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
Outdated
Show resolved
Hide resolved
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
Outdated
Show resolved
Hide resolved
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
Outdated
Show resolved
Hide resolved
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
Outdated
Show resolved
Hide resolved
b10c536
to
4340df4
Compare
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
Outdated
Show resolved
Hide resolved
5f49a95
to
f434566
Compare
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst
Outdated
Show resolved
Hide resolved
6d8b442
to
f7f570b
Compare
This PR improves the docs for this check to include an example of hidden narrowing conversions from the integer promotion rules in arithmetic.