108 lines
2.1 KiB
Plaintext
108 lines
2.1 KiB
Plaintext
include::../summary.adoc[]
|
|
|
|
== Why is this an issue?
|
|
|
|
include::../description.adoc[]
|
|
|
|
=== Exceptions
|
|
|
|
The rule does not raise an issue on statements containing only a semicolon (``++;++``).
|
|
|
|
== How to fix it
|
|
|
|
include::../how-to-fix.adoc[]
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,cpp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
int add(int a, int b) {
|
|
int result = 0;
|
|
a + b; // Noncompliant: no side effect, hides a bug, the developer likely accidentally duplicated the line
|
|
return result;
|
|
}
|
|
----
|
|
|
|
[source,cpp,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
int mul(int a, int b) {
|
|
int result = 1;
|
|
result == a; // Noncompliant: no side effect, hides a bug, the developer intended to assign
|
|
result *= b;
|
|
return result;
|
|
}
|
|
----
|
|
|
|
[source,cpp,diff-id=3,diff-type=noncompliant]
|
|
----
|
|
int sub(int a, int b) {
|
|
int result = a - b;
|
|
a - b; // Noncompliant: no side effect, there is no underlying bug, but the statement is useless
|
|
return result;
|
|
}
|
|
----
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
[source,cpp,diff-id=1,diff-type=compliant]
|
|
----
|
|
int add(int a, int b) {
|
|
int result = a + b;
|
|
return result;
|
|
}
|
|
----
|
|
|
|
[source,cpp,diff-id=2,diff-type=compliant]
|
|
----
|
|
int mul(int a, int b) {
|
|
int result = 1;
|
|
result = a;
|
|
result *= b;
|
|
return result;
|
|
}
|
|
----
|
|
|
|
[source,cpp,diff-id=3,diff-type=compliant]
|
|
----
|
|
int sub(int a, int b) {
|
|
int result = a - b;
|
|
return result;
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Standards
|
|
|
|
* CWE - https://cwe.mitre.org/data/definitions/482[CWE-482 Comparing instead of Assigning]
|
|
* CERT - https://wiki.sei.cmu.edu/confluence/x/5dUxBQ[MSC12-C. Detect and remove code that has no effect or is never executed]
|
|
|
|
=== External coding guidelines
|
|
|
|
* MISRA C:2004, 14.2 - All non-null statements shall either have at least one side-effect however executed or cause control flow to change.
|
|
|
|
=== Related rules
|
|
|
|
* S1116 - Empty statements should be removed
|
|
* S1854 - Unused assignments should be removed
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|