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

strictUnusedVariable only support _ suppression prefix #854

Merged
merged 2 commits into from
Sep 19, 2019
Merged
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 @@ -115,7 +115,7 @@
severity = WARNING,
documentSuppression = false)
public final class StrictUnusedVariable extends BugChecker implements BugChecker.CompilationUnitTreeMatcher {
private static final ImmutableSet<String> EXEMPT_PREFIXES = ImmutableSet.of("unused", "_");
private static final ImmutableSet<String> EXEMPT_PREFIXES = ImmutableSet.of("_");

/**
* The set of annotation full names which exempt annotated element from being reported as unused.
Expand Down Expand Up @@ -578,7 +578,13 @@ private void renameByIndex(List<? extends VariableTree> trees) {
// TODO(b/118437729): handle bogus source positions in enum declarations
return;
}
fix.replace(startPos, endPos, "_" + tree.getName());
if (tree.getName().toString().startsWith("unused")) {
fix.replace(startPos, endPos, "_" +
CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL,
tree.getName().toString().substring("unused".length())));
} else {
fix.replace(startPos, endPos, "_" + tree.getName());
}
}
}.scan(state.getPath().getCompilationUnit(), null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ public void handles_enums() {
"}").doTest();
}

@Test
public void renames_previous_suppression() {
refactoringTestHelper
.addInputLines(
"Test.java",
"class Test {",
" public void publicMethod(String unusedValue, String unusedValue2) { }",
" public void varArgs(String unusedValue, String... unusedValue2) { }",
"}")
.addOutputLines(
"Test.java",
"class Test {",
" public void publicMethod(String _value, String _value2) { }",
" public void varArgs(String _value, String... _value2) { }",
"}")
.doTest(TestMode.TEXT_MATCH);
}

@Test
public void renames_unused_param() {
refactoringTestHelper
Expand Down Expand Up @@ -134,11 +152,11 @@ public void fixes_suppressed_but_used_variables() {
"Test.java",
"class Test {",
" private static int _field = 1;",
" public static void privateMethod(int _value, int unusedValue2) {",
" public static void privateMethod(int _value, int _value2) {",
" int _value3 = 1;",
" _value3 = 2;",
" System.out.println(_value);",
" System.out.println(unusedValue2 + _value3 + _field);",
" System.out.println(_value2 + _value3 + _field);",
" }",
"}")
.addOutputLines(
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-854.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: StrictUnusedVariable can only be suppressed with `_` prefix
links:
- https://github.com/palantir/gradle-baseline/pull/854