From 17d11934c46c8d2c5d86fe6238b556018efc1f8b Mon Sep 17 00:00:00 2001 From: stecto01 Date: Fri, 5 Aug 2016 00:13:05 +0200 Subject: [PATCH] Allow to skip analyze for specific variable names. --- .../plugins/core/VariableNameChecker.java | 55 ++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/cflint/plugins/core/VariableNameChecker.java b/src/main/java/com/cflint/plugins/core/VariableNameChecker.java index 9779c5603..f64510e4b 100644 --- a/src/main/java/com/cflint/plugins/core/VariableNameChecker.java +++ b/src/main/java/com/cflint/plugins/core/VariableNameChecker.java @@ -1,5 +1,9 @@ package com.cflint.plugins.core; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + import com.cflint.BugList; import com.cflint.plugins.CFLintScannerAdapter; import com.cflint.plugins.Context; @@ -13,8 +17,41 @@ @Extension public class VariableNameChecker extends CFLintScannerAdapter { public static final String VARIABLE = "Variable "; + + private static final List DEFAULT_EXCLUSIONS = Collections.singletonList("rc"); + private static final String PARAM_EXCLUSION_LIST = "ExclusionList"; + + private final List exclusions = new ArrayList<>(); + final String severity = "INFO"; - + + @Override + public void setParameter(String name, String value) { + super.setParameter(name, value); + + populateExclusions(); + } + + private void populateExclusions() { + exclusions.clear(); + + final String exclusionList = getParameter(PARAM_EXCLUSION_LIST); + + if (exclusionList == null) { + exclusions.addAll(DEFAULT_EXCLUSIONS); + return; + } + + final String[] split = exclusionList.split(","); + + for (String name : split) { + final String normalizedValue = normalize(name); + if (normalizedValue != null) { + exclusions.add(normalizedValue); + } + } + } + public void expression(final CFExpression expression, final Context context, final BugList bugs) { if (expression instanceof CFVarDeclExpression) { final CFVarDeclExpression cfVarDeclExpression = (CFVarDeclExpression)expression; @@ -36,6 +73,10 @@ else if (expression instanceof CFIdentifier) { } public void checkNameForBugs(final Context context, String variable, String filename, String functionName, int line, BugList bugs) { + if (excludeFromAnalyse(variable)) { + return; + } + int minVarLength = ValidName.MIN_VAR_LENGTH; int maxVarLength = ValidName.MAX_VAR_LENGTH; int maxVarWords = ValidName.MAX_VAR_WORDS; @@ -89,4 +130,16 @@ public void checkNameForBugs(final Context context, String variable, String file context.addMessage("VAR_HAS_PREFIX_OR_POSTFIX", variable); } } + + protected boolean excludeFromAnalyse(String variable) { + return exclusions.contains(normalize(variable)); + } + + protected String normalize(String name) { + if (name == null || name.trim().isEmpty()) { + return null; + } + + return name.trim().toLowerCase(); + } } \ No newline at end of file