diff --git a/src/dscanner/analysis/incorrect_infinite_range.d b/src/dscanner/analysis/incorrect_infinite_range.d index 2949ac2b..a92c1d25 100644 --- a/src/dscanner/analysis/incorrect_infinite_range.d +++ b/src/dscanner/analysis/incorrect_infinite_range.d @@ -62,6 +62,8 @@ final class IncorrectInfiniteRangeCheck : BaseAnalyzer override void visit(const ReturnStatement rs) { + if (inStruct == 0 || line == size_t.max) // not within a struct yet + return; if (!rs.expression || rs.expression.items.length != 1) return; UnaryExpression unary = cast(UnaryExpression) rs.expression.items[0]; @@ -82,7 +84,7 @@ private: uint inStruct; enum string KEY = "dscanner.suspicious.incorrect_infinite_range"; enum string MESSAGE = "Use `enum bool empty = false;` to define an infinite range."; - size_t line; + size_t line = size_t.max; size_t column; } @@ -123,8 +125,6 @@ class C { bool empty() { return false; } } // [warn]: %1$s }c .format(IncorrectInfiniteRangeCheck.MESSAGE), sac); - - stderr.writeln("Unittest for IncorrectInfiniteRangeCheck passed."); } // test for https://github.com/dlang-community/D-Scanner/issues/656 @@ -136,3 +136,23 @@ version(none) struct Foo return; } } + +unittest +{ + import std.stdio : stderr; + import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig; + import std.format : format; + + StaticAnalysisConfig sac = disabledConfig(); + sac.incorrect_infinite_range_check = Check.enabled; + assertAnalyzerWarnings(q{ + enum isAllZeroBits = () + { + if (true) + return true; + else + return false; + }(); + }, sac); + stderr.writeln("Unittest for IncorrectInfiniteRangeCheck passed."); +}