Skip to content

Commit

Permalink
Modify S1068: Migrate to LayC - unused private fields
Browse files Browse the repository at this point in the history
Co-authored-by: Renaud T. <[email protected]>
  • Loading branch information
1 parent 82ea93b commit 281206d
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 97 deletions.
19 changes: 3 additions & 16 deletions rules/S1068/cfamily/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,17 @@

include::../description.adoc[]

=== Noncompliant code example

[source,cpp]
----
class MyClass {
private:
int foo = 42; // Noncompliant, foo is unused
public:
int compute(int a) {
return a * 42;
}
};
----

=== Compliant solution
[source,cpp]
----
class MyClass {
public:
int compute(int a) {
return a * 42;
}
int publicField = 0; // Compliant: might be used somewhere else
private:
int foo = 42; // Noncompliant: foo is unused and should be removed
};
----

Expand Down
6 changes: 5 additions & 1 deletion rules/S1068/description.adoc
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
If a ``++private++`` field is declared but not used in the program, it can be considered dead code and should therefore be removed. This will improve maintainability because developers will not wonder what the variable is used for.
If a `private` field is declared but not used locally, its limited visibility makes it dead code.

This is either a sign that some logic is missing or that the code should be cleaned.

Cleaning out dead code decreases the size of the maintained codebase, making it easier to understand and preventing bugs from being introduced.
15 changes: 1 addition & 14 deletions rules/S1068/flex/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,17 @@

include::../description.adoc[]

=== Noncompliant code example

[source,flex]
----
public class MyClass {
private var foo:int = 4; //foo is unused
private var foo:int = 4; // Noncompliant: foo is unused and should be removed
public function compute(a:int):int{
return a * 4;
}
}
----

=== Compliant solution

[source,flex]
----
public class MyClass {
public function compute(a:int):int{
return a * 4;
}
}
----

ifdef::env-github,rspecator-view[]

Expand Down
33 changes: 10 additions & 23 deletions rules/S1068/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
== Why is this an issue?

If a ``++private++`` field is declared but not used in the program, it can be considered dead code and should therefore be removed. This will improve maintainability because developers will not wonder what the variable is used for.


Note that this rule does not take reflection into account, which means that issues will be raised on ``++private++`` fields that are only accessed using the reflection API.

=== Noncompliant code example
include::../description.adoc[]

[source,java]
----
public class MyClass {
private int foo = 42;
private int foo = 42; // Noncompliant: foo is unused and should be removed
public int compute(int a) {
return a * 42;
Expand All @@ -19,42 +14,34 @@ public class MyClass {
}
----

=== Compliant solution

[source,java]
----
public class MyClass {
public int compute(int a) {
return a * 42;
}
}
----
Note that this rule does not take reflection into account, which means that issues will be raised on ``++private++`` fields that are only accessed using the reflection API.

=== Exceptions

The rule admits 3 exceptions:

* Serialization id fields
* Serialization ID fields

The Java serialization runtime associates with each serializable class a version number, called ``++serialVersionUID++``, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.
The Java serialization runtime associates with each serializable class a version number called `serialVersionUID`, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible for serialization.

A serializable class can declare its own ``++serialVersionUID++`` explicitly by declaring a field named ``++serialVersionUID++`` that must be static, final, and of type long. By definition those ``++serialVersionUID++`` fields should not be reported by this rule:
A serializable class can declare its own `serialVersionUID` explicitly by declaring a field named `serialVersionUID` that must be static, final, and of type long. By definition, those `serialVersionUID` fields should not be reported by this rule:


[source,java]
----
public class MyClass implements java.io.Serializable {
private static final long serialVersionUID = 42L;
private static final long serialVersionUID = 42L; // Compliant by exception
}
----

* Annotated fields

The unused field in this class will not be reported by the rule as it is annotated.
[source,java]
----
public class MyClass {
@SomeAnnotation
private int unused;
private int unused; // Compliant by exception
}
----

Expand All @@ -64,7 +51,7 @@ The unused field in this class will not be reported by the rule as it might be u
[source,java]
----
public class MyClass {
private int unused = 42;
private int unused = 42; // Compliant by exception
private native static void doSomethingNative();
}
----
Expand Down
16 changes: 1 addition & 15 deletions rules/S1068/php/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,10 @@

include::../description.adoc[]

=== Noncompliant code example

[source,php]
----
class MyClass {
private $foo = 4; //foo is unused
public function compute($a) {
return $a * 4;
}
}
----

=== Compliant solution

[source,php]
----
class MyClass {
private $foo = 4; // Noncompliant: foo is unused and should be removed
public function compute($a) {
return $a * 4;
Expand Down
13 changes: 1 addition & 12 deletions rules/S1068/rpg/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
== Why is this an issue?

If a variable is declared but not used in the program, it can be considered dead code and should therefore be removed. This will improve maintainability because developers will not wonder what the variable is used for.

=== Noncompliant code example
include::../description.adoc[]

[source,rpg]
----
Expand All @@ -13,15 +11,6 @@ D X S 5P 0 INZ
C EVAL I = I + 1
----

=== Compliant solution

[source,rpg]
----
D I S 5P 0 INZ
C EVAL I = I + 1
----

ifdef::env-github,rspecator-view[]

'''
Expand Down
3 changes: 0 additions & 3 deletions rules/S1068/rule.adoc

This file was deleted.

15 changes: 2 additions & 13 deletions rules/S1068/vb6/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
== Why is this an issue?

If a private variable is declared but not used in the program, it can be considered dead code and should therefore be removed. This will improve maintainability because developers will not wonder what the variable is used for.

=== Noncompliant code example
include::../description.adoc[]

[source,vb6]
----
Private Foo as Integer
Private Foo as Integer 'Noncompliant: Foo is unused and should be removed
Function Compute(A As Integer)
Compute = A * 42
End Function
----

=== Compliant solution

[source,vb6]
----
Function Compute(A As Integer)
Compute = A * 42
End Function
----

ifdef::env-github,rspecator-view[]

'''
Expand Down

0 comments on commit 281206d

Please sign in to comment.