Lorenzo Fiorini wrote:
Thanks to great Mindaugas' -w3 addition I've done a major cleanup of my apps.

I wonder if the compiler can detect if a local var is used before assigned.

function main
local nVar
nVar := nVar + 1
return nVar

Hi,


it can detect, but...

In Harbour variables are (kind of) assigned to NIL on its' defnintion, and this fact is often used by programmers. We often do, something like:

  LOCAL xI
  FOR nI := 1 TO ..
     IF xI == NIL  // xI is unassigned on first cycle of the loop
         ...
     ENDIF
     ...
  NEXT
or:
   PROC MY_MD5FILE(...)
   LOCAL cMD5
   DO WHILE ( cBuf := READBUF(...) ) != NIL
      cMD5 := HB_MD5( cBuf, cMD5 ) // cMD5 is unassigned on first cycle
   ENDDO
   RETURN cMD5

So,
   LOCAL nVar
   nVar := nVar + 1
is kind of
   LOCAL nVar := NIL
   nVar := nVar + 1
problem.

But it is not problem of unassigned variable, it is problem of operation plus (NIL + NUMERIC). Some error detection could be done now, but expression tree and general infrastructure of typed variables/values in compiler can help to find much more errors, ex.:
  nVar := NIL
  nVar += LEN(aI)
So, I do not want to do a partial implementation of this based on pcode analysis.


Best regards,
Mindaugas

_______________________________________________
Harbour mailing list
Harbour@harbour-project.org
http://lists.harbour-project.org/mailman/listinfo/harbour

Reply via email to