Skip to content

[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

Conversation

JonasToth
Copy link
Member

This PR improves the docs for this check to include an example of hidden narrowing conversions from the integer promotion rules in arithmetic.

@llvmbot
Copy link
Member

llvmbot commented Dec 1, 2024

@llvm/pr-subscribers-clang-tidy

@llvm/pr-subscribers-clang-tools-extra

Author: Jonas Toth (JonasToth)

Changes

This 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:

  • (modified) clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines/narrowing-conversions.rst (+15)
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
 -------

@JonasToth
Copy link
Member Author

@PiotrZSL I did assign you as reviewer because I noticed you do many reviews. Please remove yourself if that was not appropriate.
I did not contribute to clang-tidy for a longer period and not sure who to assign (if any!) for the PR. It was not meant offensively or so :)

Copy link
Member

@PiotrZSL PiotrZSL left a 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.

@JonasToth JonasToth force-pushed the clang-tidy/docs/improve-narrowing-conversion-docs branch from b10c536 to 4340df4 Compare December 2, 2024 21:07
@JonasToth JonasToth force-pushed the clang-tidy/docs/improve-narrowing-conversion-docs branch from 5f49a95 to f434566 Compare December 3, 2024 18:38
@JonasToth JonasToth requested a review from PiotrZSL December 4, 2024 12:14
@JonasToth JonasToth force-pushed the clang-tidy/docs/improve-narrowing-conversion-docs branch from 6d8b442 to f7f570b Compare December 15, 2024 17:40
@JonasToth JonasToth merged commit a9034d0 into llvm:main Dec 20, 2024
9 checks passed
@JonasToth JonasToth deleted the clang-tidy/docs/improve-narrowing-conversion-docs branch December 20, 2024 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] Noisy cppcoreguidelines-narrowing-conversions warning for types subject to arithmetic conversion
5 participants