Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 82e5456

Browse files
committed
CPP: Add qhelp.
1 parent 45cd030 commit 82e5456

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
double getWidth();
2+
3+
void f() {
4+
int width = getWidth();
5+
6+
// ...
7+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>This rule finds function calls whose result type is a floating point type, which are implicitly cast to an integral type. Such code may not behave as intended when the floating point return value has a fractional part, or takes an extreme value outside the range that can be represented by the integer type.</p>
8+
9+
</overview>
10+
<recommendation>
11+
<p>Consider changing the surrounding expression to match the floating point type. If rounding is intended, explicitly round using a standard function such as `trunc`, `floor` or `round`.</p>
12+
13+
</recommendation>
14+
<example><sample src="LossyFunctionResultCast.cpp" />
15+
<p>In this example, the result of the call to <code>getWidth()</code> is implicitly cast to <code>int</code>, resulting in an unintended loss of accuracy. To fix this, the type of variable <code>width</code> could be changed from <code>int</code> to <code>double</code>.</p>
16+
17+
</example>
18+
<references>
19+
20+
<li>
21+
Microsoft Visual C++ Documentation: <a href="https://docs.microsoft.com/en-us/cpp/cpp/type-conversions-and-type-safety-modern-cpp?view=vs-2017">Type Conversions and Type Safety (Modern C++)</a>.
22+
</li>
23+
<li>
24+
Cplusplus.com: <a href="http://www.cplusplus.com/doc/tutorial/typecasting/">Type conversions</a>.
25+
</li>
26+
27+
</references>
28+
</qhelp>

0 commit comments

Comments
 (0)