Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#296 InnerAssignment for if, while and do

open
nobody
None
5
2012-10-10
2004-05-03
Daniel Grenner
No

The InnerAssignment check should preferably check for
assignment of boolean values in the expression clause of
if, while and do.

An example is:
if (a = true) {...}

which should be marked as a problem, since the
developer probably wanted to do:
if (a == true) {...}

(which still is not good coding practice, but it is not as
dangerous as the first case)

Assignment to boolean literals should always be marked
as a problem, and I think the same should be true if the
assignment is done between variables. This is since it
would be difficult / confusing to read the code if both
sides are variables, e.g. with:
if (a = b).

Any comments? I wonder if it should be a modification to
the InnerAssignment check, or if it should be
implemented as a separate check, since the other
InnerAssignment checks are not concerned with the
type information. The check must not give false
positives for statements like:
while (i++ < x)...
where assignments to non-booleans are done inside the
evaluated statement.

I can do the implementation if there is an interest in this
check.

/Daniel Grenner.

Discussion

  • Lars Kühne
    Lars Kühne
    2004-06-02

    Logged In: YES
    user_id=401384

    I don't really understand why type information is necessary
    or why the check should be limited to assignment between
    literals/variables. Consider the following sample code, I
    think it should be marked as well:

    if (myCurrentState = obj.someFunkyState())
    {
    // code that should only be executed if both states are equal
    }

    My feeling is that this RFE can be implemented as a
    modification to the InnerAssignment check. A patch would be
    most welcome.