Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Remove unused local variable declarations #4028

Conversation

lyxell
Copy link
Contributor

@lyxell lyxell commented Jul 5, 2021

This pull request fixes 4 violations of Sonar rule S1481 (Unused local variables should be removed).

This patch was generated automatically using Logifix.

@slarse
Copy link
Collaborator

slarse commented Jul 5, 2021

It appears as if removing those local variables caused some imports to no longer be used:

[ERROR] src/main/java/spoon/pattern/internal/node/ElementNode.java:17:8: Unused import - spoon.pattern.internal.matcher.Matchers. [UnusedImports]
[ERROR] src/main/java/spoon/reflect/visitor/CommentHelper.java:16:8: Unused import - java.util.List. [UnusedImports]
[ERROR] src/main/java/spoon/support/reflect/declaration/CtElementImpl.java:22:8: Unused import - spoon.reflect.declaration.CtShadowable. [UnusedImports]

Reproduce with `logifix --in-place --accept=S1128 src/main/`
@lyxell
Copy link
Contributor Author

lyxell commented Jul 6, 2021

Fixed in Logifix v0.3.0.

Running logifix --patch --accept=S1481 src/main/java/spoon/pattern/internal/node/ElementNode.java src/main/java/spoon/reflect/visitor/CommentHelper.java src/main/java/spoon/reflect/visitor/ImportScannerImpl.java src/main/java/spoon/support/reflect/declaration/CtElementImpl.java on 66db36c now gives:

diff --git a/src/main/java/spoon/pattern/internal/node/ElementNode.java b/src/main/java/spoon/pattern/internal/node/ElementNode.java
@@ -14,7 +14,6 @@ import spoon.metamodel.MetamodelProperty;
 import spoon.pattern.Quantifier;
 import spoon.pattern.internal.DefaultGenerator;
 import spoon.pattern.internal.ResultHolder;
-import spoon.pattern.internal.matcher.Matchers;
 import spoon.pattern.internal.matcher.TobeMatched;
 import spoon.pattern.internal.parameter.ParameterInfo;
 import spoon.reflect.declaration.CtElement;
@@ -122,7 +121,6 @@ public class ElementNode extends AbstractPrimitiveMatcher {
         List<MapEntryNode> constantMatchers = new ArrayList<>(map.size());
         //collect Entries with variable matcher keys
         List<MapEntryNode> variableMatchers = new ArrayList<>();
-        Matchers last = null;
         for (Map.Entry<?, ?> entry : map.entrySet()) {
             MapEntryNode mem = new MapEntryNode(
                     create(entry.getKey(), patternElementToSubstRequests),
diff --git a/src/main/java/spoon/reflect/visitor/CommentHelper.java b/src/main/java/spoon/reflect/visitor/CommentHelper.java
@@ -13,7 +13,6 @@ import spoon.reflect.code.CtJavaDocTag;
 import spoon.support.Internal;
 
 import java.util.Collection;
-import java.util.List;
 import java.util.function.Function;
 import java.util.regex.Pattern;
 
@@ -96,7 +95,6 @@ public class CommentHelper {
             }
         }
         if (comment instanceof CtJavaDoc) {
-            List<CtJavaDocTag> tags = null;
             Collection<CtJavaDocTag> javaDocTags = ((CtJavaDoc) comment).getTags();
             if (javaDocTags != null && javaDocTags.isEmpty() == false) {
                 printer.write(transfo.apply("")).writeln();
diff --git a/src/main/java/spoon/reflect/visitor/ImportScannerImpl.java b/src/main/java/spoon/reflect/visitor/ImportScannerImpl.java
@@ -772,7 +772,6 @@ public class ImportScannerImpl extends CtScanner implements ImportScanner {
 
         if (parent != null) {
             CtBlock block = (CtBlock) parent;
-            boolean innerClass = false;
 
             // now we have the first container block, we want to check if we're not in an inner class
             while (parent != null && !(parent instanceof CtClass)) {
diff --git a/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java b/src/main/java/spoon/support/reflect/declaration/CtElementImpl.java
@@ -19,7 +19,6 @@ import spoon.reflect.declaration.CtAnnotation;
 import spoon.reflect.declaration.CtCompilationUnit;
 import spoon.reflect.declaration.CtElement;
 import spoon.reflect.declaration.CtNamedElement;
-import spoon.reflect.declaration.CtShadowable;
 import spoon.reflect.declaration.CtType;
 import spoon.reflect.declaration.ParentNotInitializedException;
 import spoon.reflect.factory.Factory;
@@ -170,9 +169,6 @@ public abstract class CtElementImpl implements CtElement, Serializable {
 
     @Override
     public List<CtAnnotation<? extends Annotation>> getAnnotations() {
-        if (this instanceof CtShadowable) {
-            CtShadowable shadowable = (CtShadowable) this;
-        }
         return unmodifiableList(annotations);
     }

@slarse slarse changed the title review: fix: Remove unused local variable declarations review: refactor: Remove unused local variable declarations Jul 6, 2021
@monperrus
Copy link
Collaborator

LGTM, 👍 for Logifix.

@monperrus monperrus changed the title review: refactor: Remove unused local variable declarations refactor: Remove unused local variable declarations Jul 7, 2021
@monperrus monperrus merged commit d7b8daa into INRIA:master Jul 7, 2021
@monperrus
Copy link
Collaborator

Thanks @lyxell, that's good evaluation data for Logifix.

@monperrus monperrus mentioned this pull request Aug 19, 2021
woutersmeenk pushed a commit to woutersmeenk/spoon that referenced this pull request Aug 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants