fix #533 - "Could be declared const" should be avoided for declarations with "new" merged-on-behalf-of: Richard Andrew Cattermole <alphaglosined@gmail.com>
This commit is contained in:
parent
668e2ba2e2
commit
f48862d7a5
|
@ -58,6 +58,8 @@ class UnmodifiedFinder : BaseAnalyzer
|
||||||
{
|
{
|
||||||
if (initializedFromCast(d.initializer))
|
if (initializedFromCast(d.initializer))
|
||||||
continue;
|
continue;
|
||||||
|
if (initializedFromNew(d.initializer))
|
||||||
|
continue;
|
||||||
tree[$ - 1].insert(new VariableInfo(d.name.text, d.name.line,
|
tree[$ - 1].insert(new VariableInfo(d.name.text, d.name.line,
|
||||||
d.name.column, isValueTypeSimple(dec.type)));
|
d.name.column, isValueTypeSimple(dec.type)));
|
||||||
}
|
}
|
||||||
|
@ -77,6 +79,8 @@ class UnmodifiedFinder : BaseAnalyzer
|
||||||
{
|
{
|
||||||
if (initializedFromCast(part.initializer))
|
if (initializedFromCast(part.initializer))
|
||||||
continue;
|
continue;
|
||||||
|
if (initializedFromNew(part.initializer))
|
||||||
|
continue;
|
||||||
tree[$ - 1].insert(new VariableInfo(part.identifier.text,
|
tree[$ - 1].insert(new VariableInfo(part.identifier.text,
|
||||||
part.identifier.line, part.identifier.column));
|
part.identifier.line, part.identifier.column));
|
||||||
}
|
}
|
||||||
|
@ -211,6 +215,19 @@ private:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool initializedFromNew(const Initializer initializer)
|
||||||
|
{
|
||||||
|
if (initializer.nonVoidInitializer &&
|
||||||
|
initializer.nonVoidInitializer.assignExpression &&
|
||||||
|
cast(UnaryExpression) initializer.nonVoidInitializer.assignExpression)
|
||||||
|
{
|
||||||
|
const UnaryExpression ue =
|
||||||
|
cast(UnaryExpression) initializer.nonVoidInitializer.assignExpression;
|
||||||
|
return ue.newExpression !is null;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool initializedFromCast(const Initializer initializer)
|
bool initializedFromCast(const Initializer initializer)
|
||||||
{
|
{
|
||||||
import std.typecons : scoped;
|
import std.typecons : scoped;
|
||||||
|
@ -325,6 +342,12 @@ bool isValueTypeSimple(const Type type) pure nothrow @nogc
|
||||||
StaticAnalysisConfig sac = disabledConfig();
|
StaticAnalysisConfig sac = disabledConfig();
|
||||||
sac.could_be_immutable_check = Check.enabled;
|
sac.could_be_immutable_check = Check.enabled;
|
||||||
|
|
||||||
|
// fails
|
||||||
|
|
||||||
|
assertAnalyzerWarnings(q{
|
||||||
|
void foo(){int i = 1;} // [warn]: Variable i is never modified and could have been declared const or immutable.
|
||||||
|
}, sac);
|
||||||
|
|
||||||
// pass
|
// pass
|
||||||
|
|
||||||
assertAnalyzerWarnings(q{
|
assertAnalyzerWarnings(q{
|
||||||
|
@ -339,4 +362,12 @@ bool isValueTypeSimple(const Type type) pure nothrow @nogc
|
||||||
void foo(){enum i = 1;}
|
void foo(){enum i = 1;}
|
||||||
}, sac);
|
}, sac);
|
||||||
|
|
||||||
|
assertAnalyzerWarnings(q{
|
||||||
|
void foo(){E e = new E;}
|
||||||
|
}, sac);
|
||||||
|
|
||||||
|
assertAnalyzerWarnings(q{
|
||||||
|
void foo(){auto e = new E;}
|
||||||
|
}, sac);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue