|
21 | 21 | import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; |
22 | 22 | import static com.google.errorprone.matchers.Description.NO_MATCH; |
23 | 23 | import static com.google.errorprone.util.ASTHelpers.getStartPosition; |
| 24 | +import static com.google.errorprone.util.ASTHelpers.hasExplicitSource; |
24 | 25 |
|
25 | 26 | import com.google.common.base.Preconditions; |
26 | 27 | import com.google.common.base.Strings; |
@@ -67,29 +68,44 @@ public class ParameterName extends BugChecker |
67 | 68 | implements MethodInvocationTreeMatcher, NewClassTreeMatcher { |
68 | 69 |
|
69 | 70 | private final ImmutableList<String> exemptPackages; |
| 71 | + private final boolean matchImplicitSource; |
70 | 72 |
|
71 | 73 | @Inject |
72 | | - ParameterName(ErrorProneFlags errorProneFlags) { |
| 74 | + ParameterName(ErrorProneFlags flags) { |
73 | 75 | this.exemptPackages = |
74 | | - errorProneFlags.getListOrEmpty("ParameterName:exemptPackagePrefixes").stream() |
| 76 | + flags.getListOrEmpty("ParameterName:exemptPackagePrefixes").stream() |
75 | 77 | // add a trailing '.' so that e.g. com.foo matches as a prefix of com.foo.bar, but not |
76 | 78 | // com.foobar |
77 | 79 | .map(p -> p.endsWith(".") ? p : p + ".") |
78 | 80 | .collect(toImmutableList()); |
| 81 | + this.matchImplicitSource = flags.getBoolean("ParameterName:matchImplicitSource").orElse(true); |
79 | 82 | } |
80 | 83 |
|
81 | 84 | @Override |
82 | 85 | public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { |
83 | | - checkArguments(tree, tree.getArguments(), state.getEndPosition(tree.getMethodSelect()), state); |
| 86 | + checkArguments( |
| 87 | + tree, tree.getArguments(), argListStartPosition(tree.getMethodSelect(), state), state); |
84 | 88 | return NO_MATCH; |
85 | 89 | } |
86 | 90 |
|
87 | 91 | @Override |
88 | 92 | public Description matchNewClass(NewClassTree tree, VisitorState state) { |
89 | | - checkArguments(tree, tree.getArguments(), state.getEndPosition(tree.getIdentifier()), state); |
| 93 | + checkArguments( |
| 94 | + tree, tree.getArguments(), argListStartPosition(tree.getIdentifier(), state), state); |
90 | 95 | return NO_MATCH; |
91 | 96 | } |
92 | 97 |
|
| 98 | + int argListStartPosition(Tree tree, VisitorState state) { |
| 99 | + if (!matchImplicitSource && !hasExplicitSource(tree, state)) { |
| 100 | + return Position.NOPOS; |
| 101 | + } |
| 102 | + int pos = state.getEndPosition(tree); |
| 103 | + if (pos != Position.NOPOS) { |
| 104 | + return pos; |
| 105 | + } |
| 106 | + return getStartPosition(tree); |
| 107 | + } |
| 108 | + |
93 | 109 | private void checkArguments( |
94 | 110 | Tree tree, |
95 | 111 | List<? extends ExpressionTree> arguments, |
|
0 commit comments