-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA0-1-1.cpp
More file actions
89 lines (75 loc) · 1.92 KB
/
A0-1-1.cpp
File metadata and controls
89 lines (75 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Rule: A0-1-1
// Source line: 1427
// Original file: A0-1-1.cpp
//% $Id: A0-1-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <array>
#include <cstdint>
std::uint8_t Fn1(std::uint8_t param) noexcept
{
std::int32_t x{0}; // Non-compliant - DU data flow anomaly; Variable defined,
// but not used
if (param > 0)
{
return 1;
}
else
{
return 0;
}
}
std::int32_t Fn2() noexcept
{
std::int8_t x{10U}; // Compliant - variable defined and will be used
std::int8_t y{20U}; // Compliant - variable defined and will be used
std::int16_t result = x + y; // x and y variables used
x = 0; // Non-compliant - DU data flow anomaly; Variable defined, but x is
// not subsequently used and goes out of scope
y = 0; // Non-compliant - DU data flow anomaly; Variable defined, but y is
// not subsequently used and goes out of scope
return result;
}
std::int32_t Fn3(std::int32_t param) noexcept
{
std::int32_t x{param +
1}; // Compliant - variable defined, and will be used in
// one of the branches
// However, scope of x variable could be reduced
if (param > 20)
{
return x;
}
return 0;
}
std::int32_t Fn4(std::int32_t param) noexcept
{
std::int32_t x{param +
1}; // Compliant - variable defined, and will be used in
// some of the branches
if (param > 20)
{
return x + 1;
}
else if (param > 10)
{
return x;
}
else
{
return 0;
}
}
void Fn5() noexcept
{
std::array<std::int32_t, 100> arr{};
arr.fill(1);
constexpr std::uint8_t limit{100U};
std::int8_t x{0};
for (std::uint8_t i{0U}; i < limit; ++i) // Compliant by exception - on the
// final loop, value of i defined will
// not be used
{
arr[i] = arr[x];
++x; // Non-compliant - DU data flow anomaly on the final loop, value
// defined and not used
}
}