Modify rule S4143: Adapt to LaYC (#1931)

This commit is contained in:
Fred Tingaud 2023-06-12 14:18:01 +02:00 committed by GitHub
parent 9e3cc3533a
commit 28657dcc74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 39 additions and 23 deletions

View File

@ -2,14 +2,16 @@
include::../description.adoc[] include::../description.adoc[]
=== Noncompliant code example {intro}
[source,cpp] [source,cpp]
---- ----
towns[i] = "London"; towns[i] = "London";
towns[i] = "Chicago"; // Noncompliant towns[i] = "Chicago"; // Noncompliant: We never used the previous value
---- ----
{outro}
ifdef::env-github,rspecator-view[] ifdef::env-github,rspecator-view[]
''' '''

View File

@ -2,7 +2,7 @@
include::../description.adoc[] include::../description.adoc[]
=== Noncompliant code example {intro}
[source,csharp] [source,csharp]
---- ----
@ -13,6 +13,8 @@ dictionary.Add(key, "value 1");
dictionary[key] = "value 2"; // Noncompliant dictionary[key] = "value 2"; // Noncompliant
---- ----
{outro}
ifdef::env-github,rspecator-view[] ifdef::env-github,rspecator-view[]
''' '''

View File

@ -1 +1,3 @@
It is highly suspicious when a value is saved for a key or index and then unconditionally overwritten. Such replacements are likely errors. :intro: Storing a value inside a collection at a given key or index and then unconditionally overwriting it without reading the initial value is a case of "dead store".
:outro: At best it is redundant and will confuse the reader, most often it is an error and not what the developer intended to do.

View File

@ -2,7 +2,7 @@
include::../description.adoc[] include::../description.adoc[]
=== Noncompliant code example {intro}
[source,go] [source,go]
---- ----
@ -17,6 +17,8 @@ towns[i] = "London"
towns[i] = "Chicago" // Noncompliant towns[i] = "Chicago" // Noncompliant
---- ----
{outro}
ifdef::env-github,rspecator-view[] ifdef::env-github,rspecator-view[]
''' '''

View File

@ -1,21 +1,25 @@
== Why is this an issue? == Why is this an issue?
It is highly suspicious when a value is saved for a key or index and then unconditionally overwritten. Such replacements are likely in error. include::../description.adoc[]
=== Noncompliant code example {intro}
This rule detects repeatedly adding an element at the same index or key in a collection or adding identical elements to a set.
[source,javascript] [source,javascript]
---- ----
fruits[1] = "banana"; fruits[1] = "banana";
fruits[1] = "apple"; // Noncompliant - value on index 1 is overwritten fruits[1] = "apple"; // Noncompliant
myMap.set("key", 1); myMap.set("key", 1);
myMap.set("key", 2); // Noncompliant - value for key "key" is replaced myMap.set("key", 2); // Noncompliant
mySet.add(1); mySet.add(1);
mySet.add(1); // Noncompliant - element is already in the set mySet.add(1); // Noncompliant
---- ----
{outro}
ifdef::env-github,rspecator-view[] ifdef::env-github,rspecator-view[]
''' '''

View File

@ -2,14 +2,16 @@
include::../description.adoc[] include::../description.adoc[]
=== Noncompliant code example {intro}
[source,php] [source,php]
---- ----
$someArray[1] = "someValue"; $someArray[1] = "someValue";
$someArray[1] = "someOtherValue"; // The intention here was probably to write to another array key. $someArray[1] = "someOtherValue"; // Noncompliant
---- ----
{outro}
ifdef::env-github,rspecator-view[] ifdef::env-github,rspecator-view[]
''' '''

View File

@ -1,13 +1,8 @@
== Why is this an issue? == Why is this an issue?
:link-with-uscores1: https://docs.python.org/3/reference/datamodel.html#object.__setitem__ include::../description.adoc[]
It is highly suspicious when a value is saved in a collection for a given key or index and then unconditionally overwritten. Such replacements are likely errors. {intro}
This rule raises an issue when the {link-with-uscores1}[``++__setitem__++``] method of the same object is called multiple times with the same index, slice or key without any other action done between the calls.
=== Noncompliant code example
[source,python] [source,python]
---- ----
@ -25,6 +20,8 @@ mymap['a']['b'] = 42
mymap['a']['b'] = 42 # Noncompliant mymap['a']['b'] = 42 # Noncompliant
---- ----
{outro}
ifdef::env-github,rspecator-view[] ifdef::env-github,rspecator-view[]
''' '''

View File

@ -2,9 +2,9 @@
include::description.adoc[] include::description.adoc[]
=== Noncompliant code example {intro}
[source,text] [source,java]
---- ----
letters.put("a", "Apple"); letters.put("a", "Apple");
letters.put("a", "Boy"); // Noncompliant letters.put("a", "Boy"); // Noncompliant
@ -13,3 +13,4 @@ towns[i] = "London";
towns[i] = "Chicago"; // Noncompliant towns[i] = "Chicago"; // Noncompliant
---- ----
{outro}

View File

@ -2,7 +2,7 @@
include::../description.adoc[] include::../description.adoc[]
=== Noncompliant code example {intro}
[source,swift] [source,swift]
---- ----
@ -13,6 +13,8 @@ towns[i] = "London"
towns[i] = "Chicago" // Noncompliant towns[i] = "Chicago" // Noncompliant
---- ----
{outro}
ifdef::env-github,rspecator-view[] ifdef::env-github,rspecator-view[]
''' '''

View File

@ -2,7 +2,7 @@
include::../description.adoc[] include::../description.adoc[]
=== Noncompliant code example {intro}
[source,vbnet] [source,vbnet]
---- ----
@ -10,6 +10,8 @@ towns.Item(x) = "London"
towns.Item(x) = "Chicago"; // Noncompliant towns.Item(x) = "Chicago"; // Noncompliant
---- ----
{outro}
ifdef::env-github,rspecator-view[] ifdef::env-github,rspecator-view[]
''' '''