Fix #241
This commit is contained in:
parent
54dca3c4da
commit
93d2bc17d8
|
@ -84,6 +84,8 @@ you do not want to use the one created by the "--defaultConfig" option.
|
||||||
* Redundant parenthesis.
|
* Redundant parenthesis.
|
||||||
* Unused labels.
|
* Unused labels.
|
||||||
* Lines longer than 120 characters.
|
* Lines longer than 120 characters.
|
||||||
|
* Incorrect infinite range definitions.
|
||||||
|
* Some assertions that check conditions that will always be true.
|
||||||
|
|
||||||
#### Wishlist
|
#### Wishlist
|
||||||
|
|
||||||
|
|
|
@ -92,7 +92,7 @@ unittest
|
||||||
import std.format : format;
|
import std.format : format;
|
||||||
|
|
||||||
StaticAnalysisConfig sac;
|
StaticAnalysisConfig sac;
|
||||||
sac.builtin_property_names_check = true;
|
sac.incorrect_infinite_range_check = true;
|
||||||
assertAnalyzerWarnings(q{struct InfiniteRange
|
assertAnalyzerWarnings(q{struct InfiniteRange
|
||||||
{
|
{
|
||||||
bool empty() // [warn]: %1$s
|
bool empty() // [warn]: %1$s
|
||||||
|
|
|
@ -28,14 +28,11 @@ class UselessAssertCheck : BaseAnalyzer
|
||||||
override void visit(const AssertExpression ae)
|
override void visit(const AssertExpression ae)
|
||||||
{
|
{
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
|
import std.string : removechars;
|
||||||
|
|
||||||
UnaryExpression unary = cast(UnaryExpression) ae.assertion;
|
UnaryExpression unary = cast(UnaryExpression) ae.assertion;
|
||||||
if (unary is null)
|
if (unary is null)
|
||||||
{
|
|
||||||
stderr.writeln("unary is null");
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if (unary.primaryExpression is null)
|
if (unary.primaryExpression is null)
|
||||||
return;
|
return;
|
||||||
immutable token = unary.primaryExpression.primary;
|
immutable token = unary.primaryExpression.primary;
|
||||||
|
@ -45,11 +42,11 @@ class UselessAssertCheck : BaseAnalyzer
|
||||||
if (!skipSwitch) switch (token.type)
|
if (!skipSwitch) switch (token.type)
|
||||||
{
|
{
|
||||||
case tok!"doubleLiteral":
|
case tok!"doubleLiteral":
|
||||||
if (!token.text.to!double)
|
if (!token.text.removechars("Ll").to!double)
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
case tok!"floatLiteral":
|
case tok!"floatLiteral":
|
||||||
if (!token.text.to!float)
|
if (!token.text.removechars("Ff").to!float)
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
case tok!"idoubleLiteral":
|
case tok!"idoubleLiteral":
|
||||||
|
@ -61,7 +58,7 @@ class UselessAssertCheck : BaseAnalyzer
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
case tok!"longLiteral":
|
case tok!"longLiteral":
|
||||||
if (!token.text.to!long)
|
if (!token.text.removechars("Ll").to!long)
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
case tok!"realLiteral":
|
case tok!"realLiteral":
|
||||||
|
@ -69,11 +66,11 @@ class UselessAssertCheck : BaseAnalyzer
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
case tok!"uintLiteral":
|
case tok!"uintLiteral":
|
||||||
if (!token.text.to!uint)
|
if (!token.text.removechars("Uu").to!uint)
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
case tok!"ulongLiteral":
|
case tok!"ulongLiteral":
|
||||||
if (!token.text.to!ulong)
|
if (!token.text.removechars("UuLl").to!ulong)
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
case tok!"characterLiteral":
|
case tok!"characterLiteral":
|
||||||
|
@ -88,9 +85,35 @@ class UselessAssertCheck : BaseAnalyzer
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
addErrorMessage(ae.line, ae.column, KEY, "Assert condition is always true");
|
addErrorMessage(ae.line, ae.column, KEY, MESSAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum string KEY = "dscanner.suspicious.useless_assert";
|
enum string KEY = "dscanner.suspicious.useless_assert";
|
||||||
|
enum string MESSAGE = "Assert condition is always true.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import std.stdio : stderr;
|
||||||
|
import analysis.config : StaticAnalysisConfig;
|
||||||
|
import std.format : format;
|
||||||
|
|
||||||
|
StaticAnalysisConfig sac;
|
||||||
|
sac.useless_assert_check = true;
|
||||||
|
assertAnalyzerWarnings(q{
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
assert(true); // [warn]: %1$s
|
||||||
|
assert(1); // [warn]: %1$s
|
||||||
|
assert([10]); // [warn]: %1$s
|
||||||
|
assert(false);
|
||||||
|
assert(0);
|
||||||
|
assert(0.0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
}c
|
||||||
|
.format(UselessAssertCheck.MESSAGE), sac);
|
||||||
|
stderr.writeln("Unittest for UselessAssertCheck passed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue