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

Add support for specifying exemptPrefixes/exemptNames for UnusedVariable via flags #2753

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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