From 93d2bc17d84011467c639ef0f6e4dd2abc5ec230 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Wed, 4 Nov 2015 17:20:01 -0800 Subject: [PATCH] Fix #241 --- README.md | 2 ++ src/analysis/incorrect_infinite_range.d | 2 +- src/analysis/useless_assert.d | 43 +++++++++++++++++++------ 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ef05da3..e926458 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,8 @@ you do not want to use the one created by the "--defaultConfig" option. * Redundant parenthesis. * Unused labels. * Lines longer than 120 characters. +* Incorrect infinite range definitions. +* Some assertions that check conditions that will always be true. #### Wishlist diff --git a/src/analysis/incorrect_infinite_range.d b/src/analysis/incorrect_infinite_range.d index 9e808e6..0e3ded0 100644 --- a/src/analysis/incorrect_infinite_range.d +++ b/src/analysis/incorrect_infinite_range.d @@ -92,7 +92,7 @@ unittest import std.format : format; StaticAnalysisConfig sac; - sac.builtin_property_names_check = true; + sac.incorrect_infinite_range_check = true; assertAnalyzerWarnings(q{struct InfiniteRange { bool empty() // [warn]: %1$s diff --git a/src/analysis/useless_assert.d b/src/analysis/useless_assert.d index 18d4296..d4c9d1e 100644 --- a/src/analysis/useless_assert.d +++ b/src/analysis/useless_assert.d @@ -28,14 +28,11 @@ class UselessAssertCheck : BaseAnalyzer override void visit(const AssertExpression ae) { import std.conv : to; + import std.string : removechars; UnaryExpression unary = cast(UnaryExpression) ae.assertion; if (unary is null) - { - stderr.writeln("unary is null"); return; - } - if (unary.primaryExpression is null) return; immutable token = unary.primaryExpression.primary; @@ -45,11 +42,11 @@ class UselessAssertCheck : BaseAnalyzer if (!skipSwitch) switch (token.type) { case tok!"doubleLiteral": - if (!token.text.to!double) + if (!token.text.removechars("Ll").to!double) return; break; case tok!"floatLiteral": - if (!token.text.to!float) + if (!token.text.removechars("Ff").to!float) return; break; case tok!"idoubleLiteral": @@ -61,7 +58,7 @@ class UselessAssertCheck : BaseAnalyzer return; break; case tok!"longLiteral": - if (!token.text.to!long) + if (!token.text.removechars("Ll").to!long) return; break; case tok!"realLiteral": @@ -69,11 +66,11 @@ class UselessAssertCheck : BaseAnalyzer return; break; case tok!"uintLiteral": - if (!token.text.to!uint) + if (!token.text.removechars("Uu").to!uint) return; break; case tok!"ulongLiteral": - if (!token.text.to!ulong) + if (!token.text.removechars("UuLl").to!ulong) return; break; case tok!"characterLiteral": @@ -88,9 +85,35 @@ class UselessAssertCheck : BaseAnalyzer default: return; } - addErrorMessage(ae.line, ae.column, KEY, "Assert condition is always true"); + addErrorMessage(ae.line, ae.column, KEY, MESSAGE); } private: 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."); +} +