2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-06-08 14:23:48 +02:00
Calculating or retrieving a value only to then overwrite it or throw it away, could indicate a serious error in the code. Even if it's not an error, it is at best a waste of resources. Therefore all calculated values should be used.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-06-08 14:23:48 +02:00
2022-02-04 17:28:24 +01:00
[source,html]
2021-06-08 14:23:48 +02:00
----
void calculateRate(int a, int b)
{
int i;
i = a + b; // Noncompliant; calculation result not used before value is overwritten
i = doSomething(); // Noncompliant; retrieved value not used
for (i = 0; i < 10; i++) {
// ...
}
// ...
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-06-08 14:23:48 +02:00
2022-02-04 17:28:24 +01:00
[source,html]
2021-06-08 14:23:48 +02:00
----
void calculateRate(int a, int b)
{
int i;
i = doSomething();
i += a + b;
storeI(i)
for (i = 0; i < 10; i++) {
// ...
}
}
----
ifdef::env-github,rspecator-view[]
2021-06-08 15:52:13 +02:00
'''
2021-06-08 14:23:48 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== duplicates: S1854
=== on 19 Sep 2014, 13:54:47 Freddy Mallet wrote:
@Ann, I would:
* Associated this rule to CWE-563: \http://cwe.mitre.org/data/definitions/563.html
* Use the SQALE "Reliability" characteristic because again when there is a potential operational risk, this is more important than anything else
* Add the tag "Bug"
2021-06-08 14:23:48 +02:00
endif::env-github,rspecator-view[]