Hinweise zu häufigen Warnungen der automatischen Kontrolle


CODE FORMATTING:
In codeblocks "Plugins" --> "Source code formatter";  oder astyle direkt aufrufen.

warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]

Tritt häufig bei Verwendung von 'float'-Variablen auf.
Ursache: 3.0 etc. ist eine double-Zahl,  3.0f ist ein float-Zahl

Lösung:

(a) warning: implicit conversion changes signedness: 'int' to 'unsigned int' [-Wsign-conversion]
(b) warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]

int n1, n2;
...
for(unsigned int i=n1; i<=n2; ++i)

                         (a)   (b)
Lösung:

warning: comparing floating point with == or != is unsafe [-Wfloat-equal]

float D, E;
...
if (D==E)           // Nur True falls jedes bit identisch ist.
                    // Das kann bei Gleitkommzahlen wegen Rundungsfehlern evtl. nie eintreten


(performance) Function parameter 's' should be passed by reference.

bool is_palindrom(const string s)
{ ... }


Lösung: Statt der Parameterübergabe per Value (wie oben) empfiehlt der Compiler eine Übergabe per Referenz, also
bool is_palindrom(const string& s)

Vorteil: Weniger Speicherbedarf und Code wird schneller, da nichts kopiert werden muß. Lohnt sich für alle Container (vector, string, list ,...) als auch für eigene Klassen.
 warning: variable ‘x’ set but not used [-Wunused-but-set-variable]
   29 |     double x;

   
Lösung:

(style) Variable 't' is assigned a value that is never used.

Lösung:

(style) The scope of the variable 'd' can be reduced.

float d;
...
if ()
{       // d wird nur in diesem Scope benutzt
  d = ...
  x = sqrt(d+2);
}       // d wird außerhalb dieses Scopes nicht mehr benutzt.


Lösung:  Reduce the scope of variable 'd' (to if-branch in this example):
...
if ()
{
  float d = ...
  x = sqrt(d+2);
}


Hinweis: Nutzen Sie die Möglichkeit von C++ Variablen erst dort zu deklarieren wo diese gebraucht werden (kleinstmöglicher Scope).

note: use '==' to turn this assignment into an equality comparison
    }while(Bedingung = 0);

Typischer Anfängerfehler (oder Wechsel der Programmiersprache).
Statt eines Vergleichs wird "==" wird eine Zuweisung "=" benutzt.

Lösung:
    }while(0 == Bedingung);    // Sicherer: konstanter Ausdruck als linker Operand (keine Zuweisung möglich)
warning: declaration of ‘a’ shadows a previous local [-Wshadow]

Eine Deklaration eines inneren Gültigkeitsbereichs/Scopes überdeckt (shadows) eine Deklaration des äußeren Scopes

Typische Situation I:
int a=-2;      // outer scope variable
float b;       // outer scope variable
...
for (int a=0; ...) // inner scope variable
{
   double b; ...   // inner scope variable
   cout << a << b ;// inner scope variables are used
}
cout << a << b ; // outer scope variables are valid again



Typische Situation II (analog zu I):
void fkt(int a, float b)  // outer scope variables
{
   int a;                         // inner scope variable
   double b; ...                  // inner scope variable
   cout << a << b ;               // inner scope variables are used
}


Lösung: Umbenennen der äußeren oder inneren Variablen (oder Löschen derselben falls unnötig).

warning: implicit conversion turns floating-point number into integer:
    int n;
    ....
    int k5 = floor(n/5);


Lösung:

warning: this statement may fall through [-Wimplicit-fallthrough=]
     case 's': v = partial_sum(m);
     case 't': .... ;


Lösung: Ein Fall (case) muß mit 'break;' abgeschlossen werden. Ansonsten wird der nächstfolgende Fall ebenfalls ausgeführt.
     case 's': v = partial_sum(m); break;
     case 't': .... ;break;


Hinweis: Falls ein solches "fall through" erwünscht ist, dann kann dies mit [[fallthrough]];  gekennzeichnet werden (C++-17).

warning: suggest braces around empty body in an ‘if’ statement [-Wempty-body]
     if (i==1);
        {...}               // als if-Zweig gedacht


Ursache: Obiges Codefragment wird als
     if (i==1) { };
     {...}               // als if-Zweig gedacht

gelesen, d.h. der "empty body" {} wird als Anweisungsblock des if-Zweiges genommen und der dafür gedachte Block wird unabhängig vom Test '1==i' stets ausgeführt.

Lösung: Weg mit dem ';' .

Aufpassen: Dasselbe passiert bei

[2020-05-02]
dd
dd
dd