diff --git a/.dscanner.ini b/.dscanner.ini index 16b69e9228c..75f658e2797 100644 --- a/.dscanner.ini +++ b/.dscanner.ini @@ -91,6 +91,8 @@ allman_braces_check="enabled" redundant_attributes_check="enabled" ; Check for public declarations without a documented unittest has_public_example="enabled" +; Check for asserts without an explanatory message +assert_without_msg="skip-unittest" ; Configure which modules are checked with a specific checker ; Please help to extend these checks onto more Phobos modules @@ -111,6 +113,8 @@ alias_syntax_check="-std.traits,-std.typecons" allman_braces_check="+disabled" ; Checks for confusing code in inline asm statements asm_style_check="-std.math" +; Check for asserts without an explanatory message +assert_without_msg="-etc.c.SQL_,-std.algorithm.comparison,-std.algorithm.iteration,-std.algorithm.searching,-std.algorithm.setops,-std.algorithm.sorting,-std.array,-std.bigint,-std.bitmanip,-std.concurrency,-std.container,-std.container.array,-std.container.binaryheap,-std.container.dlist,-std.container.rbtree,-std.container.slist,-std.conv,-std.csv,-std.datetime,-std.datetime.date,-std.datetime.interval,-std.datetime.systime,-std.datetime.timezone,-std.digest.digest,-std.digest.murmurhash,-std.digest.sha,-std.encoding,-std.exception,-std.experimental.allocator,-std.experimental.allocator.building_blocks.affix_allocator,-std.experimental.allocator.building_blocks.allocator_list,-std.experimental.allocator.building_blocks.bitmapped_block,-std.experimental.allocator.building_blocks.bucketizer,-std.experimental.allocator.building_blocks.free_list,-std.experimental.allocator.building_blocks.free_tree,-std.experimental.allocator.building_blocks.kernighan_ritchie,-std.experimental.allocator.building_blocks.null_allocator,-std.experimental.allocator.building_blocks.quantizer,-std.experimental.allocator.building_blocks.region,-std.experimental.allocator.building_blocks.scoped_allocator,-std.experimental.allocator.common,-std.experimental.allocator.gc_allocator,-std.experimental.allocator.mallocator,-std.experimental.allocator.mmap_allocator,-std.experimental.allocator.typed,-std.experimental.checkedint,-std.file,-std.format,-std.functional,-std.getopt,-std.internal.cstring,-std.internal.digest.sha_SSSE3,-std.internal.math.biguintcore,-std.internal.math.biguintnoasm,-std.internal.math.gammafunction,-std.internal.scopebuffer,-std.math,-std.mathspecial,-std.mmfile,-std.net.curl,-std.numeric,-std.outbuffer,-std.parallelism,-std.path,-std.process,-std.random,-std.range,-std.range.primitives,-std.regex,-std.regex.internal.backtracking,-std.regex.internal.generator,-std.regex.internal.ir,-std.regex.internal.kickstart,-std.regex.internal.parser,-std.regex.internal.thompson,-std.signals,-std.socket,-std.stdio,-std.string,-std.traits,-std.typecons,-std.uni,-std.uri,-std.utf,-std.uuid,-std.variant,-std.windows.registry,-std.xml" ; Checks for assignment to auto-ref function parameters auto_ref_assignment_check="-std.algorithm.mutation,-std.format,-std.typecons" ; Checks for variables that could be declared immutable diff --git a/std/algorithm/comparison.d b/std/algorithm/comparison.d index be4f7f89ef7..9bd57cda7c7 100644 --- a/std/algorithm/comparison.d +++ b/std/algorithm/comparison.d @@ -533,7 +533,7 @@ auto clamp(T1, T2, T3)(T1 val, T2 lower, T3 upper) in { import std.functional : greaterThan; - assert(!lower.greaterThan(upper)); + assert(!lower.greaterThan(upper), "Lower can't be greater than upper."); } do { @@ -732,7 +732,7 @@ Compares two ranges for equality, as defined by predicate $(D pred) template equal(alias pred = "a == b") { enum isEmptyRange(R) = - isInputRange!R && __traits(compiles, {static assert(R.empty);}); + isInputRange!R && __traits(compiles, {static assert(R.empty, "");}); enum hasFixedLength(T) = hasLength!T || isNarrowString!T; diff --git a/std/algorithm/mutation.d b/std/algorithm/mutation.d index b684f8fd79e..19039f45be0 100644 --- a/std/algorithm/mutation.d +++ b/std/algorithm/mutation.d @@ -257,7 +257,7 @@ if (isInputRange!InputRange && isForwardRange!ForwardRange) } else { - assert(front.empty); + assert(front.empty, "Expected front to be empty"); // Left side was shorter. Let's step into the back. static if (is(InputRange == Take!ForwardRange)) { @@ -1528,7 +1528,7 @@ private InputRange2 moveAllImpl(alias moveOp, InputRange1, InputRange2)( && hasSlicing!InputRange2 && isRandomAccessRange!InputRange2) { auto toMove = src.length; - assert(toMove <= tgt.length); + assert(toMove <= tgt.length, "Source buffer needs to be smaller or equal to the target buffer."); foreach (idx; 0 .. toMove) moveOp(src[idx], tgt[idx]); return tgt[toMove .. tgt.length]; @@ -1537,7 +1537,7 @@ private InputRange2 moveAllImpl(alias moveOp, InputRange1, InputRange2)( { for (; !src.empty; src.popFront(), tgt.popFront()) { - assert(!tgt.empty); + assert(!tgt.empty, "Source buffer needs to be smaller or equal to the target buffer."); moveOp(src.front, tgt.front); } return tgt; @@ -1849,7 +1849,7 @@ if (s != SwapStrategy.stable break; } // Advance to next blackout on the left - assert(blackouts[left].pos >= tgtPos); + assert(blackouts[left].pos >= tgtPos, "Next blackout on the left shouldn't appear before the target."); tgt.popFrontExactly(blackouts[left].pos - tgtPos); tgtPos = blackouts[left].pos; diff --git a/std/digest/package.d b/std/digest/package.d index 33fb5bf1202..3f3bc15f1ac 100644 --- a/std/digest/package.d +++ b/std/digest/package.d @@ -931,7 +931,7 @@ if (isDigest!T) : Digest nothrow ubyte[] finish(ubyte[] buf) in { - assert(buf.length >= this.length); + assert(buf.length >= this.length, "Given buffer is smaller than the local buffer."); } do { @@ -967,7 +967,7 @@ if (isDigest!T) : Digest @trusted ubyte[] peek(ubyte[] buf) const in { - assert(buf.length >= this.length); + assert(buf.length >= this.length, "Given buffer is smaller than the local buffer."); } do { diff --git a/std/json.d b/std/json.d index 8ba0f05f782..9204de682ca 100644 --- a/std/json.d +++ b/std/json.d @@ -1166,7 +1166,7 @@ string toJSON(const ref JSONValue root, in bool pretty = false, in JSONOptions o // Make sure we do UTF decoding iff we want to // escape Unicode characters. assert(((options & JSONOptions.escapeNonAsciiChars) != 0) - == is(Char == dchar)); + == is(Char == dchar), "JSONOptions.escapeNonAsciiChars needs dchar strings"); with (JSONOptions) if (isControl(c) || ((options & escapeNonAsciiChars) >= escapeNonAsciiChars && c >= 0x80)) diff --git a/std/zip.d b/std/zip.d index 762d62b483d..6b2c5f2e742 100644 --- a/std/zip.d +++ b/std/zip.d @@ -390,7 +390,7 @@ final class ZipArchive import std.zlib : crc32; () @trusted { de._crc32 = crc32(0, cast(void[]) de._expandedData); }(); } - assert(de._compressedData.length == de._compressedSize); + assert(de._compressedData.length == de._compressedSize, "Archive member compressed failed."); } /** Delete de from the archive. @@ -543,7 +543,7 @@ final class ZipArchive i += 22; // Write archive comment - assert(i + comment.length == data.length); + assert(i + comment.length == data.length, "Writing the archive comment failed."); _data[i .. data.length] = (cast(ubyte[]) comment)[]; return cast(void[]) data; diff --git a/std/zlib.d b/std/zlib.d index 651b4f9d1fb..ce14f3c29fa 100644 --- a/std/zlib.d +++ b/std/zlib.d @@ -191,7 +191,7 @@ uint crc32(uint crc, const(void)[] buf) ubyte[] compress(const(void)[] srcbuf, int level) in { - assert(-1 <= level && level <= 9); + assert(-1 <= level && level <= 9, "Compression level needs to be within [-1, 9]."); } do { @@ -276,7 +276,7 @@ void[] uncompress(const(void)[] srcbuf, size_t destlen = 0u, int winbits = 15) throw new ZlibException(err); } } - assert(0); + assert(0, "Unreachable code"); } @system unittest @@ -370,7 +370,7 @@ class Compress this(int level, HeaderFormat header = HeaderFormat.deflate) in { - assert(1 <= level && level <= 9); + assert(1 <= level && level <= 9, "Legal compression level are in [1, 9]."); } do { @@ -461,7 +461,8 @@ class Compress void[] flush(int mode = Z_FINISH) in { - assert(mode == Z_FINISH || mode == Z_SYNC_FLUSH || mode == Z_FULL_FLUSH); + assert(mode == Z_FINISH || mode == Z_SYNC_FLUSH || mode == Z_FULL_FLUSH, + "Mode must be either Z_FINISH, Z_SYNC_FLUSH or Z_FULL_FLUSH."); } do { @@ -572,7 +573,7 @@ class UnCompress const(void)[] uncompress(const(void)[] buf) in { - assert(!done); + assert(!done, "Buffer has been flushed."); } do { @@ -752,11 +753,11 @@ class UnCompress void[] flush() in { - assert(!done); + assert(!done, "Buffer has been flushed before."); } out { - assert(done); + assert(done, "Flushing failed."); } do {