Skip to content

Commit 060dada

Browse files
committed
False positive warning "Function ... is never used" #214
1 parent cc5dd7a commit 060dada

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/main/java/intellij_awk/psi/AwkStringMixin.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,43 @@ public AwkStringMixin(@NotNull ASTNode node) {
2121
private static final Pattern stringThatCanBeFunctionName =
2222
Pattern.compile("\"[A-Za-z_][A-Za-z0-9_]*\"");
2323

24-
static boolean canBeFunctionName(String s) {
25-
return stringThatCanBeFunctionName.matcher(s).matches();
26-
}
27-
2824
@Override
2925
public String getName() {
26+
return getPossibleFunctionName();
27+
}
28+
29+
@Nullable
30+
private String getPossibleFunctionName() {
3031
String str = getText();
3132
return canBeFunctionName(str) ? unquoteString(str) : null;
3233
}
3334

35+
static boolean canBeFunctionName(String s) {
36+
return stringThatCanBeFunctionName.matcher(s).matches();
37+
}
38+
3439
public PsiElement setName(String newName) {
3540
return replaceNameNode(AwkElementFactory.createFunctionCallName(getProject(), newName));
3641
}
3742

3843
@Override
3944
public PsiReference getReference() {
40-
return getName() != null && isRhsOfAssignment()
45+
return getPossibleFunctionName() != null && canBeFunctionReference()
4146
? new AwkReferenceFunction(this, TextRange.from(1, getTextLength() - 2))
4247
: null;
4348
}
4449

50+
private boolean canBeFunctionReference() {
51+
return isRhsOfAssignment() || isUserFunctionArgument();
52+
}
53+
54+
/** `f("fname")` but not `substr("fname")` (built-in) */
55+
private boolean isUserFunctionArgument() {
56+
AwkGawkFuncCallList callList = AwkUtil.findParent(this, AwkGawkFuncCallList.class);
57+
return callList != null && callList.getParent() instanceof AwkFunctionCallUser;
58+
}
59+
60+
/** `a = "fname"` */
4561
private boolean isRhsOfAssignment() {
4662
return AwkUtil.isType(AwkUtil.getPrevNotWhitespace(getParent()), AwkTypes.ASSIGN);
4763
}

0 commit comments

Comments
 (0)