Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Implement #6172: parameter/argument warning #6183

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -4336,6 +4336,8 @@ else if (arg instanceof Tree.Comprehension) {
else {
checkPositionalArgument(param, pr,
(Tree.ListedArgument) arg);
checkPositionalArgumentPosition(
i, param, args);
}
}
}
Expand Down Expand Up @@ -4743,6 +4745,48 @@ private void checkPositionalArgument(Parameter p,
}
}

/**
* Warns if argument and parameter names don't match (#6172)
*/
private void checkPositionalArgumentPosition(
int paramIndex, Parameter param, List<Tree.PositionalArgument> args) {
String paramName = param.getName();
if (paramIndex < args.size()) {
Tree.PositionalArgument currentArg = args.get(paramIndex);
if (currentArg instanceof Tree.ListedArgument) {
Tree.Term currentArgTerm = unwrapExpressionUntilTerm(
((Tree.ListedArgument)currentArg).getExpression());
if (currentArgTerm instanceof Tree.BaseMemberExpression) {
String currentArgName = ((Tree.BaseMemberExpression)currentArgTerm)
.getIdentifier().getText();
if (currentArgName.equals(paramName)) {
return;
}
}
}
}
for (int otherArgIndex = 0; otherArgIndex < args.size(); otherArgIndex++) {
if (otherArgIndex == paramIndex) continue;
Tree.PositionalArgument otherArg = args.get(otherArgIndex);
if (otherArg instanceof Tree.ListedArgument) {
Tree.Term otherArgTerm = unwrapExpressionUntilTerm(
((Tree.ListedArgument)otherArg).getExpression());
if (otherArgTerm instanceof Tree.BaseMemberExpression) {
String otherArgName = ((Tree.BaseMemberExpression)otherArgTerm)
.getIdentifier().getText();
if (otherArgName.equals(paramName)) {
otherArg.addUsageWarning(
Warning.suspiciousArgument,
"argument '" + otherArgName +
"' does not assign to parameter '" +
paramName + "'; consider using named arguments " +
"if this is intentional");
}
}
}
}
}

@Override public void visit(Tree.Comprehension that) {
super.visit(that);
Tree.InitialComprehensionClause icc =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public enum Warning {
javaAnnotationElement,
syntaxDeprecation,
smallIgnored,
literalNotSmall
}
literalNotSmall,
suspiciousArgument
}