Skip to content

Commit

Permalink
False positive warning "Function ... is never used" #214
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Apr 17, 2024
1 parent cc5dd7a commit 060dada
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/main/java/intellij_awk/psi/AwkStringMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,43 @@ public AwkStringMixin(@NotNull ASTNode node) {
private static final Pattern stringThatCanBeFunctionName =
Pattern.compile("\"[A-Za-z_][A-Za-z0-9_]*\"");

static boolean canBeFunctionName(String s) {
return stringThatCanBeFunctionName.matcher(s).matches();
}

@Override
public String getName() {
return getPossibleFunctionName();
}

@Nullable
private String getPossibleFunctionName() {
String str = getText();
return canBeFunctionName(str) ? unquoteString(str) : null;
}

static boolean canBeFunctionName(String s) {
return stringThatCanBeFunctionName.matcher(s).matches();
}

public PsiElement setName(String newName) {
return replaceNameNode(AwkElementFactory.createFunctionCallName(getProject(), newName));
}

@Override
public PsiReference getReference() {
return getName() != null && isRhsOfAssignment()
return getPossibleFunctionName() != null && canBeFunctionReference()
? new AwkReferenceFunction(this, TextRange.from(1, getTextLength() - 2))
: null;
}

private boolean canBeFunctionReference() {
return isRhsOfAssignment() || isUserFunctionArgument();
}

/** `f("fname")` but not `substr("fname")` (built-in) */
private boolean isUserFunctionArgument() {
AwkGawkFuncCallList callList = AwkUtil.findParent(this, AwkGawkFuncCallList.class);
return callList != null && callList.getParent() instanceof AwkFunctionCallUser;
}

/** `a = "fname"` */
private boolean isRhsOfAssignment() {
return AwkUtil.isType(AwkUtil.getPrevNotWhitespace(getParent()), AwkTypes.ASSIGN);
}
Expand Down

0 comments on commit 060dada

Please sign in to comment.