rspec/rules/S6780/java/rule.adoc
github-actions[bot] d9b0dfabc0
Create rule S6780: Assertion conditions should not evaluate to false (#3094)
---------

Co-authored-by: andreaguarino <andreaguarino@users.noreply.github.com>
Co-authored-by: Gyula Sallai <gyula.sallai@sonarsource.com>
2024-08-09 14:27:51 +02:00

19 lines
660 B
Plaintext

== Why is this an issue?
Assertions in Java are used to test assumptions that should hold true at a certain point in the program. An assertion with a condition that is always false will fail, which can lead to unexpected program termination. Moreover, an assertion that is always false often indicates a logic error or misunderstanding in the code, which could lead to further issues.
=== Noncompliant code example
[source,java]
----
void caller() {
// Noncompliant: triggers an AssertionError because the array length is 2
printFifth(new int[2]);
}
void printFifth(int[] a) {
assert a.length == 5;
System.out.println(a[4]);
}
----