So I recently discovered CFLint…
I’ve been going through this legacy code, trying to fix potential scope problems, and I ran into this bit:
<cfset preString="">
<cfif isDefined("URL.Mode") and len(trim(URL.Mode)) gt 0>
<cfset preString="#URL.Mode# ">
</cfif>
<CFSAVECONTENT variable="myBreadcrumbs">
<A HREF="#thisPageStruct['Level1URL']#">#thisPageStruct["Level1Text"]#</A> ->
<A HREF="#thisPageStruct['Level2URL']#">#thisPageStruct["Level2Text"]#</A> ->
<A HREF="#thisPageStruct['Level3URL']##qString#" target="iRight">#preString##thisPageStruct["Level3Text"]#</A>
</CFSAVECONTENT>
cflint warns: “Variable preString is not declared with a var statement.cflint(MISSING_VAR)”
so I changed the first line to
<cfset var preString="">
but now cflint says: “Local variable preString is not used in function getBreadcrumbs. Consider removing it.cflint(UNUSED_LOCAL_VARIABLE)”
So now I’m wondering if this is a problem with cflint’s parser or with my understanding of the code. The variable preString
is used within the <cfsavecontent>
tag…so I was wondering if <cfsavecontent>
maybe creates a new local
scope?
Then I realized that the tag’s contents also reference local variables qString
and thisPageStruct
, which are declared earlier in the code using var
. Turns out I’m getting the same UNUSED_LOCAL_VARIABLE error at
<cfset var qString = "">
Confusingly, the declaration for thisPageStruct
shows a different cflint error: “Variable has prefix or postfix thisPageStruct and could be named better.cflint(VAR_HAS_PREFIX_OR_POSTFIX)”.
CFLint could probably use better documentation for its rules but anyways… through trial and error, it appears that it disapproves of variable names like thisVal
(or myThing
or qStuff
). OK, whatever… I renamed it to page
and it’s fine…
So anyways, after cleaning up the remaining CFLint variable name complaints (or, to be honest, just disabling them…I’ll go crazy “fixing” every variable name in the website), I’ve got something like this:
<cfset var qString = "">
<cfif isDefined("CGI.QUERY_STRING") AND len(CGI.QUERY_STRING) NEQ "0">
<cfset qString="?#CGI.QUERY_STRING#">
</cfif>
<cfset var page = ...>
...
<cfif ...>
...
<cfelseif page["Level4Text"] is NULL>
<cfset preString="">
<cfif isDefined("URL.Mode") and len(trim(URL.Mode)) gt 0>
<cfset preString="#URL.Mode# ">
</cfif>
<CFSAVECONTENT variable="myBreadcrumbs">
<A HREF="#page['Level1URL']#">#page["Level1Text"]#</A> ->
<A HREF="#page['Level2URL']#">#page["Level2Text"]#</A> ->
<A HREF="#page['Level3URL']##qString#" target="iRight">#preString##page["Level3Text"]#</A>
</CFSAVECONTENT>
<cfelse>
...
I’m still getting UNUSED_LOCAL_VARIABLE for qString
and preString
but no such warnings for page
. Since page
is referenced outside of the <cfsavecontent>
tag, I’m still unsure whether this is a problem with cflint’s parser or if it is legitimately warning me about a new local scope within the tag content.
Edit: hmm, actually…if it was a new local scope, shouldn’t it warn me about accessing undeclared variables within the tag content?