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

Correctly fix previous suppressed params #865

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 @@ -144,6 +144,7 @@ public final class StrictUnusedVariable extends BugChecker implements BugChecker
"serialVersionUID",
// TAG fields are used by convention in Android apps.
"TAG");
private static final String UNUSED = "unused";

@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
Expand Down Expand Up @@ -578,10 +579,12 @@ private void renameByIndex(List<? extends VariableTree> trees) {
// TODO(b/118437729): handle bogus source positions in enum declarations
return;
}
if (tree.getName().toString().startsWith("unused")) {
fix.replace(startPos, endPos, "_" +
CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL,
tree.getName().toString().substring("unused".length())));
String name = tree.getName().toString();
if (name.startsWith(UNUSED)) {
fix.replace(startPos, endPos, "_" + (name.equals(UNUSED)
? "value"
: CaseFormat.UPPER_CAMEL.to(
CaseFormat.LOWER_CAMEL, name.substring(UNUSED.length()))));
} else {
fix.replace(startPos, endPos, "_" + tree.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,22 @@ public void fixes_suppressed_but_used_variables() {
"}"
).doTest(TestMode.TEXT_MATCH);
}

@Test
public void fixes_previously_suppressed_variables() {
refactoringTestHelper
.addInputLines(
"Test.java",
"class Test {",
" public static void privateMethod(int unused) {",
" }",
"}")
.addOutputLines(
"Test.java",
"class Test {",
" public static void privateMethod(int _value) {",
" }",
"}"
).doTest(TestMode.TEXT_MATCH);
}
}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-865.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Ensure that `StrictUnusedVariable` correctly converts previously suppressed variables `unused` to `_`
links:
- https://github.com/palantir/gradle-baseline/pull/865