Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the POM file #238

Merged
merged 1 commit into from
Feb 12, 2017
Merged
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
Expand Down
27 changes: 20 additions & 7 deletions src/main/java/com/cflint/plugins/core/UnusedArgumentChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ public class UnusedArgumentChecker extends CFLintScannerAdapter {

@Override
public void element(final Element element, final Context context, final BugList bugs) {
if (element.getName().equals("cfargument")) {
if (element.getName().equals("cfargument")) {
final String name = element.getAttributeValue("name") != null
? element.getAttributeValue("name").toLowerCase() : "";
methodArguments.put(name, false);
setArgumentLineNo(name, context.startLine());
final String code = element.getParentElement().toString();
if(isUsed(code, name)){
methodArguments.put(name, true);
}
}
}

Expand Down Expand Up @@ -57,10 +61,10 @@ protected void setArgumentLineNo(final String argument, final Integer lineNo) {
}

protected void useIdentifier(final CFFullVarExpression fullVarExpression) {
if (fullVarExpression.getExpressions().size() > 0) {
if (fullVarExpression.getExpressions().size() > 0) {
final CFExpression identifier1 = fullVarExpression.getExpressions().get(0);
if (identifier1 instanceof CFIdentifier) {
if ("arguments".equalsIgnoreCase(((CFIdentifier) identifier1).getName())
if ("arguments".equalsIgnoreCase(((CFIdentifier) identifier1).getName())
&& fullVarExpression.getExpressions().size() > 1) {
final CFExpression identifier2 = fullVarExpression.getExpressions().get(1);
if (identifier2 instanceof CFIdentifier) {
Expand All @@ -74,15 +78,15 @@ protected void useIdentifier(final CFFullVarExpression fullVarExpression) {
}

protected void useIdentifier(final CFIdentifier identifier) {
final String name = identifier.getName().toLowerCase();
final String name = identifier.getName().toLowerCase();
if (methodArguments.get(name) != null) {
methodArguments.put(name, true);
}
}

@Override
public void expression(final CFExpression expression, final Context context, final BugList bugs) {
if (expression instanceof CFFullVarExpression) {
if (expression instanceof CFFullVarExpression) {
useIdentifier((CFFullVarExpression) expression);
} else if (expression instanceof CFIdentifier) {
useIdentifier((CFIdentifier) expression);
Expand All @@ -98,13 +102,22 @@ public void startFunction(final Context context, final BugList bugs) {
@Override
public void endFunction(final Context context, final BugList bugs) {
// sort by line number
for (final Map.Entry<String, Boolean> method : methodArguments.entrySet()) {
for (final Map.Entry<String, Boolean> method : methodArguments.entrySet()) {
final String name = method.getKey();
final Boolean used = method.getValue();
if (!used) {
final String name = method.getKey();
context.addMessage("UNUSED_METHOD_ARGUMENT", name, argumentLineNo.get(name));
}
}
}

private boolean isUsed(String content, final String name) {
boolean isUsed = false;
content = content.replace(" ", "").replace("'", "\"").toLowerCase();
boolean structKeyCheck = (content.contains("arguments[\"" + name + "\"]"));
boolean isDefinedCheck = (content.contains("arguments." + name));
isUsed = structKeyCheck || isDefinedCheck;
return isUsed;
}

}
20 changes: 20 additions & 0 deletions src/test/java/com/cflint/TestUnusedArgumentChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,25 @@ public void testArgumentsUsedInTagWithArgumentsScope() throws ParseException, IO
final Map<String, List<BugInfo>> result = cfBugs.getBugs().getBugList();
assertEquals(0, result.size());
}

@Test
public void testArgumentsUsedInQuerygWithArgumentsScope() throws ParseException, IOException {
final String tagSrc = "<cfcomponent>\r\n"
+ "<cffunction name=\"getProduct\">\r\n"
+ "<cfargument name=\"productid\">\r\n"
+ "<cfargument name=\"b\">\r\n"
+ "<cfset var qryProduct = \"\" />\r\n"
+ "<cfquery name=\"qryProduct\">\r\n"
+ "SET @productId = <cfqueryparam value=\"#arguments.productid#\" cfsqltype=\"cf_sql_integer\" />\r\n"
+ "SELECT @productId as productId\r\n"
+ "</cfquery>\r\n"
+ "<cfreturn arguments.a + arguments.b>\r\n"
+ "</cffunction>\r\n"
+ "</cfcomponent>\r\n";

cfBugs.process(tagSrc, "test");
final Map<String, List<BugInfo>> result = cfBugs.getBugs().getBugList();
assertEquals(0, result.size());
}

}