gordonklaus / ineffassign

Detect ineffectual assignments in Go code.
MIT License
397 stars 23 forks source link

False positive #3 #44

Closed ruchikaguptaa closed 4 years ago

ruchikaguptaa commented 4 years ago

The linter complains ineffectual assignment to dataclientUsingSecondarykey on second assignment of dataclientUsingSecondaryKey = true. But the variable is being used again later on. Looks like bug?

    var dataclientUsingSecondaryKey bool
    containerExists, rerr := storagedataclient.ContainerExists(ctx, goal.ContainerName)
    if rerr != nil {
        dataclientUsingSecondaryKey = true
    }

    logger.TraceInfof("container '%s' exists: %t", goal.ContainerName, containerExists)

    if goal.IsDeleting {
        if rerr := storagedataclient.DeleteContainer(ctx, goal.ContainerName); rerr != nil {
            if !dataclientUsingSecondaryKey {
                dataclientUsingSecondaryKey = true
                storagedataclient.DeleteContainer(ctx, goal.ContainerName)
                }
            }
        }

        logger.TraceInfof("Delete container '%s' succeeded.", goal.ContainerName)
        return retry.Success, nil, nil
    }

    if rerr := storagedataclient.CreateContainer(ctx, goal.ContainerName, azStorage.ContainerAccessTypePrivate); rerr != nil {
        if !dataclientUsingSecondaryKey {
            logger.TraceInfof("Creating storagedataclient with secondary key since CreateContainer returned AuthenticationFailed")
        }
    }
gordonklaus commented 4 years ago

The second assignment is followed unconditionally by a return, so it is indeed ineffectual.

(There is an extra closing brace in the middle of your code, such that it does not quite parse. Please post valid a Go code snippet if you still have an issue.)

ruchikaguptaa commented 4 years ago

@gordonklaus Thanks for pointing that out and sorry for the trouble. I missed that.