Modify rule S1764: update to LaYC and sync with implementation

## Review

A dedicated reviewer checked the rule description successfully for:

- [x] logical errors and incorrect information
- [x] information gaps and missing content
- [x] text style and tone
- [x] PR summary and labels follow the guidelines

---------

Co-authored-by: Amélie Renard <44666826+amelie-renard-sonarsource@users.noreply.github.com>
This commit is contained in:
Alejandro Álvarez Ayllón 2023-09-05 16:37:24 +02:00 committed by GitHub
parent 29e3105062
commit e19f2c951d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,31 +1,6 @@
== Why is this an issue?
include::../description.adoc[]
=== Noncompliant code example
[source,cpp]
----
if ( a == a ) { // always true
do_z();
}
if ( a != a ) { // always false
do_y();
}
if ( a == b && a == b ) { // if the first one is true, the second one is too
do_x();
}
if (a == b || a == b ) { // if the first one is true, the second one is too
do_w();
}
if (5 / 5) { // always 1
do_v();
}
if (5 - 5) { // always 0
do_u();
}
----
Using the same value on both sides of a binary operator is a code defect. In the case of logical operators, it is either a copy/paste error and, therefore, a bug, or it is simply duplicated code and should be simplified. In the case of most binary mathematical operators, having the same value on both sides of an operator yields predictable results and should be simplified as well.
=== Exceptions
@ -37,10 +12,49 @@ The following are ignored:
* Arithmetic operators `+, *`
* Assignment operators `=, +=, *=`
=== Code examples
==== Noncompliant code example
[source,cpp]
----
void foo(int a, int b) {
if ( a == a ) { // Noncompliant: always true
// ...
}
if ( a != a ) { // Noncompliant: always false
// ...
}
if ( (a == b) && (a == b) ) { // Noncompliant: if the first condition is true, the second one is too
// ...
}
if ( (a == b) || (a == b) ) { // Noncompliant: if the first condition is true, the second one is too
// ...
}
if ( 5 / 5 ) { // Noncompliant: always 1
// ...
}
if ( 5 - 5 ) { // Noncompliant: always 0
// ...
}
}
----
== Resources
=== Standards
* https://wiki.sei.cmu.edu/confluence/x/5dUxBQ[CERT, MSC12-C.] - Detect and remove code that has no effect or is never executed
* S1656 - Implements a check on `=`.
=== Related rules
* S1656 detects self-assignments
ifdef::env-github,rspecator-view[]