aboutsummaryrefslogtreecommitdiff
path: root/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst
blob: b1e82cae8ca5dc095a6b4b4e3ab9cdfbaa7aba42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.. title:: clang-tidy - cppcoreguidelines-narrowing-conversions

cppcoreguidelines-narrowing-conversions
=======================================

Checks for silent narrowing conversions, e.g: ``int i = 0; i += 0.1;``. While
the issue is obvious in this former example, it might not be so in the
following: ``void MyClass::f(double d) { int_member_ += d; }``.

This rule is part of the "Expressions and statements" profile of the C++ Core
Guidelines, corresponding to rule ES.46. See

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Res-narrowing.

We enforce only part of the guideline, more specifically, we flag:
 - All floating-point to integer conversions that are not marked by an explicit
   cast (c-style or ``static_cast``). For example: ``int i = 0; i += 0.1;``,
   ``void f(int); f(0.1);``,
 - All applications of binary operators where the left-hand-side is an integer
   and the right-hand-size is a floating-point. For example:
   ``int i; i+= 0.1;``.