Skip to content

Commit

Permalink
Add support for specifying exemptPrefixes/exemptNames for UnusedVaria…
Browse files Browse the repository at this point in the history
…ble via flags

In some cases projects have conventions about use of custom arguments/variables that are not meant to be deleted even if unused. One of such examples could be keeping an ORMs `Session session` argument in the method arguments signifying that the method happens within the boundaries of provided session and is doing calls to the underlying datastore.

This PR would allow to provide custom values for exemptPrefixes and exemptNames via error-prone flags.

Fixes #2753

FUTURE_COPYBARA_INTEGRATE_REVIEW=#2753 from bezmax:unused-variable-parameters 36b9502
PiperOrigin-RevId: 566671352
  • Loading branch information
Maksim Bezsaznyj authored and Error Prone Team committed Sep 19, 2023
1 parent 5ab2545 commit 240dc8a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@
severity = WARNING,
documentSuppression = false)
public final class UnusedVariable extends BugChecker implements CompilationUnitTreeMatcher {
private static final String EXEMPT_PREFIX = "unused";
private final ImmutableSet<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 @@ -185,6 +185,14 @@ public final class UnusedVariable extends BugChecker implements CompilationUnitT
.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();

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

@Override
Expand Down Expand Up @@ -566,10 +574,11 @@ private static boolean exemptedByAnnotation(List<? extends AnnotationTree> annot
return false;
}

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

private class VariableFinder extends TreePathScanner<Void, Void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,17 @@ 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 @@ -434,7 +434,14 @@ 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 240dc8a

Please sign in to comment.