Skip to content

Warn when comparing a variable with its only possible value #961

@vpanteleev-sym

Description

@vpanteleev-sym
void example1()
{
	int counter;
	if (counter == 0)
		writeln("First run");
	counter++;
}

void example2()
{
	string[string] cache;
	if (cache is null)
		cache = populateCache();
	// ... use cache ...
}

In both cases, we are missing static from the local variable. Though we cannot infer the intent, we can at least determine and point out that comparing the variable with its only possible value at that time doesn't really make sense.

This could be something as complicated as value propagation but also as simple as just checking whether the variable has ever been initialized since it was declared.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions