Skip to content

Commit

Permalink
Fix a crash in VarChecker on effectively final resource variables
Browse files Browse the repository at this point in the history
The final modifier appears in the AST even if it was implicit in the
source.

Fixes #498

MOE_MIGRATED_REVID=149767713
  • Loading branch information
cushon committed Mar 10, 2017
1 parent d1c72b1 commit 517f76f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Expand Up @@ -89,10 +89,11 @@ private Description handleLocalOrParam(VariableTree tree, VisitorState state, Sy
if (Source.instance(state.context).allowEffectivelyFinalInInnerClasses()) {
// In Java 8, the final modifier is never necessary on locals/parameters because
// effectively final variables can be used anywhere a final variable is required.
return buildDescription(tree)
.setMessage(UNNECESSARY_FINAL)
.addFix(SuggestedFixes.removeModifiers(tree, state, Modifier.FINAL))
.build();
Fix fix = SuggestedFixes.removeModifiers(tree, state, Modifier.FINAL);
// The fix may be null for TWR variables that were not explicitly final
if (fix != null) {
return buildDescription(tree).setMessage(UNNECESSARY_FINAL).addFix(fix).build();
}
}
return Description.NO_MATCH;
}
Expand Down
Expand Up @@ -310,4 +310,20 @@ public void finalTWR() {
"}")
.doTest();
}

@Test
public void nonFinalTWR() {
compilationHelper
.addSourceLines(
"Test.java",
"import java.io.InputStream;",
"class Test {",
" public void x() {",
" try (InputStream is = null) {",
" } catch (Exception e) {",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit 517f76f

Please sign in to comment.