Skip to content

Commit

Permalink
Extract exemptPrefixes and exemptNames for UnusedVariable into parame…
Browse files Browse the repository at this point in the history
…ters
  • Loading branch information
Maksim Bezsaznyj committed Dec 13, 2021
1 parent 7f4b220 commit 36b9502
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@
severity = WARNING,
documentSuppression = false)
public final class UnusedVariable extends BugChecker implements CompilationUnitTreeMatcher {
private static final String EXEMPT_PREFIX = "unused";
private final ImmutableList<String> exemptPrefixes;

private static final ImmutableSet<String> EXEMPT_NAMES = ImmutableSet.of("ignored");
private final ImmutableSet<String> exemptNames;

/**
* The set of annotation full names which exempt annotated element from being reported as unused.
Expand Down Expand Up @@ -163,6 +163,18 @@ public UnusedVariable(ErrorProneFlags flags) {
.ifPresent(methodAnnotationsExemptingParameters::addAll);
this.methodAnnotationsExemptingParameters = methodAnnotationsExemptingParameters.build();
this.reportInjectedFields = flags.getBoolean("Unused:ReportInjectedFields").orElse(false);

ImmutableSet.Builder<String> exemptNames = ImmutableSet.<String>builder().add("ignored");
flags
.getList("Unused:exemptNames")
.ifPresent(exemptNames::addAll);
this.exemptNames = exemptNames.build();

ImmutableList.Builder<String> exemptPrefixes = ImmutableList.<String>builder().add("unused");
flags
.getList("Unused:exemptPrefixes")
.ifPresent(exemptPrefixes::addAll);
this.exemptPrefixes = exemptPrefixes.build();
}

@Override
Expand Down Expand Up @@ -542,12 +554,6 @@ private static boolean exemptedByAnnotation(
return false;
}

private static boolean exemptedByName(Name name) {
String nameString = name.toString();
return Ascii.toLowerCase(nameString).startsWith(EXEMPT_PREFIX)
|| EXEMPT_NAMES.contains(nameString);
}

private class VariableFinder extends TreePathScanner<Void, Void> {
private final Map<Symbol, TreePath> unusedElements = new HashMap<>();

Expand Down Expand Up @@ -610,6 +616,13 @@ && exemptedFieldBySuperType(getType(variableTree), state)) {
return null;
}

private boolean exemptedByName(Name name) {
String nameString = name.toString();
String nameStringLower = Ascii.toLowerCase(nameString);
return exemptPrefixes.stream().anyMatch(nameStringLower::startsWith)
|| exemptNames.contains(nameString);
}

private boolean exemptedFieldBySuperType(Type type, VisitorState state) {
return EXEMPTING_FIELD_SUPER_TYPES.stream()
.anyMatch(t -> isSubtype(type, state.getTypeFromString(t), state));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.errorprone.bugpatterns;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
Expand Down Expand Up @@ -102,10 +103,18 @@ public void exemptedByName() {
"Unuseds.java",
"package unusedvars;",
"class ExemptedByName {",
" private void unused1(int a, int unusedParam) {",
" private void unused1(" +
" int a, int unusedParam, " +
" int customUnused1, int customUnused2, " +
" int prefixUnused1Param, int prefixUnused2Param" +
" ) {",
" int unusedLocal = a;",
" }",
"}")
.setArgs(
"-XepOpt:Unused:exemptNames=customUnused1,customUnused2",
"-XepOpt:Unused:exemptPrefixes=prefixunused1,prefixunused2"
)
.doTest();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,15 @@ public void exemptedByName() {
" private int unusedInt;",
" private static final int UNUSED_CONSTANT = 5;",
" private int ignored;",
" private int customUnused1;",
" private int customUnused2;",
" private int prefixUnused1Field;",
" private int prefixUnused2Field;",
"}")
.setArgs(
"-XepOpt:Unused:exemptNames=customUnused1,customUnused2",
"-XepOpt:Unused:exemptPrefixes=prefixunused1,prefixunused2"
)
.doTest();
}

Expand Down

0 comments on commit 36b9502

Please sign in to comment.