Skip to content

Commit

Permalink
ImmutableMemberCollection: support field-level @SuppressWarnings
Browse files Browse the repository at this point in the history
Currently `@SuppressWarnings("ImmutableMemberCollection")` works at the class level but not the field level, which isn't very intuitive; this change resolves that.

Fixes #2301

COPYBARA_INTEGRATE_REVIEW=#2301 from PicnicSupermarket:improvement/ImmutableMemberCollection-field-suppression 3f3d7df
PiperOrigin-RevId: 370176353
  • Loading branch information
Stephan202 authored and Error Prone Team committed Apr 23, 2021
1 parent 5f1c735 commit c715004
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public Description matchClass(ClassTree classTree, VisitorState state) {
classTree.getMembers().stream()
.filter(member -> PRIVATE_FINAL_VAR_MATCHER.matches(member, state))
.filter(member -> !EXCLUSIONS.matches(member, state))
.filter(member -> !isSuppressed(member))
.map(VariableTree.class::cast)
.flatMap(varTree -> stream(isReplaceable(varTree, state)))
.collect(toImmutableMap(ReplaceableVar::symbol, var -> var));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ public void listInitInline_notMutated_replacesTypeWithImmutableList() {
.doTest();
}

@Test
public void setInitConstructor_notMutatedButSuppressed_doesNothing() {
refactoringHelper
.addInputLines(
"Test.java",
"import java.util.List;",
"class Test {",
" @SuppressWarnings(\"ImmutableMemberCollection\")",
" private final List<String> myList;",
" Test(List<String> myList) {",
" this.myList = myList;",
" }",
"}")
.expectUnchanged()
.doTest();
}

@Test
public void listInitConstructor_notMutated_replacesTypeWithImmutableList() {
refactoringHelper
Expand Down

0 comments on commit c715004

Please sign in to comment.