A blog post that shows some of the warnings of the Clang compiler:

http://blog.llvm.org/2013/09/clang-warnings.html

Below I copy from the blog post some of the most interesting of them, with quotations.

---------------------------------

enum SortType {
    unknown = 0,
    min_invalid = 3,

    bubble = 1,
    quick,
    insert
};


In this enum, a few non-valid values are defined, then the valid enums listed. Valid enums use the auto increment to get their values. However, min_invalid and insert both have value 3. Luckily, -Wduplicate-enum will identify enums in this situation and point them out. GCC will not warn on this.


D doesn't warn on this. Perhaps it's good to accept duplicated enum values only when the user adds some kind of annotation that denotes a desire for a duplicated value.

---------------------------------

On to sort.cc

Class constructor:
Sort::Sort(int vec[], int size, bool sorted)
    : sorted_(sorted_), vec_(vec), size_(size) {

Members from sort.h:
    int* vec_;
    bool sorted_;
    int &size_;


Checking the only constructor of the class, numerous problems can be seen here. [...] sorted_ is initialized with itself instead of with sorted. This leads to uninitialized value in sorted_, which is caught by the aptly named -Wuninitialized. For this case, GCC has -Wself-assign and -Wself-init.


In bugzilla I have asked for similar tests, like (that is closed):
http://d.puremagic.com/issues/show_bug.cgi?id=4407

Self-assignment is a common mistake that I'd like the D compiler to catch.

---------------------------------

for (int i = 0; i < size_; ++i) {
  for (int j = 1; j < size_; ++i) {
    ...
  }
}

This double nested loop gives bubble sort its n2 running time. Rather, in this case, an infinite running time. Note the increment in both of the loops happen on i, even in the inner loop. j is never touched, either here or inside the loop. -Wloop-analysis will give a warning when all the variables inside a for loop conditional does not change during the loop iteration. Only in Clang.


D has foreach that avoids this bug, but sometimes in D you have to use for loops (like when the increment is not 1)


void main() {
    enum size_ = 5;
    for (int i = 0; i < size_; i += 2) {
        for (int j = 1; j < size_; i += 2) {}
    }
}

See:
http://d.puremagic.com/issues/show_bug.cgi?id=11018

Bye,
bearophile

Reply via email to