C & C++

C & C++

Made by DeepSource
Missing unconditional break statement in switch clause CXX-C1001
Anti-pattern
Minor

Fall-through in switch cases is often considered risky. Hence consider adding an unconditional break for each switch clause.

Missing default case in switch statement CXX-W1164
Anti-pattern
Minor

Default case provides switch statements with fallback, and in general is a good to have. Hence consider adding default case to switch.

Special symbols like *, ", ', \ and /* found in header names CXX-W1207
Anti-pattern
Minor

Using special-meaning characters in header names can produce errors in parsing.

Consider cleaning up header names.

Control variable of for loop modified in body CXX-W1241
Anti-pattern
Minor

Modifying the control variable of a for loop in its body can make the code harder to read.

Consider using a while loop, or move the modification into the for loop's update expression.

Audit required: found a non-const global variable CXX-W2009
Anti-pattern
Minor

Global variables are accessible anywhere in the scope of the program even in the threads outside of the main thread, as long as it shares the main process stack.

A mutable global variable have high dependency and the dependent code on such variables have no way to learn about the changes to the variable. Hence using a non-const global variables is considered quite risky, as they can lead to data races or contention issues.

Consider avoiding the use of global variable by adding a qualifier const. If the variable must be mutable then consider passing the variable as argument to the functions which are designed to mutate the variable.

In the case of atomic or synchronization primitives being used to handle data races, there is still concern about misuse, which can be reduced by making it non-global. That is a private class member that can only be used by its function.

Pointer returned from a function is dereferenced on LHS of an assignement CXX-W1230
Anti-pattern
Minor

When a function returns a pointer to dynamically allocated memory, it is the responsibility of the caller to free that memory after it is no longer needed. But if the caller deferences such a pointer on the left-hand side of an assignment expression, the caller loses the oppurtunity to free the memory.

Iteration expression is outside of for loop CXX-W1240
Anti-pattern
Minor

The following is the syntax of a for loop.

attr(optional) for ( declaration-or-expression(optional) ; condition(optional) ; iteration-expression(optional) ) statement

Though the last part of the loop i.e. "iteration-expression" is optional, missing it out or moving it to the body of the for loop defeats the purpose of using a for loop.

If such is the requirement, consider using a while.

Switch statement with a single-clause CXX-W1197
Anti-pattern
Minor

Switch statements should have more than one clause for them to be any more useful over an if-else construct. Consider rewriting the switch as if-else, thus making the code more readable.

Implicit deletion of copy and move assignment constructors due to non-static const data member CXX-W2010
Anti-pattern
Minor

Const or reference data members are only allowed to be initialized by the constructor of the class, and never after. And unlike in other languages, they are still instance dependent types. Having such members is rarely useful, and makes the class only copy-constructible but not copy-assignable.

Side effect in array index CXX-W1247
Anti-pattern
Minor

A side effect refers to a modification of the state of a program or system that is outside the intended computation. Having a side-effect in an array index have multiple drawbacks. Array index with side effect(s) can be difficult to read and understand, especially for other programmers who may need to maintain or modify the code in the future. It can also make debugging more difficult.

Loop body is not enclosed in {} CXX-W1243
Anti-pattern
Minor

Loop body not being enclosed in {} can make it confusing and hard to read, hence consider using {} for loop body.

Use of sizeof with an expression as operand CXX-W1188
Anti-pattern
Minor

The operator sizeof never evaluates the expression provided as an argument unless it's a variable length array. It only determines the type of the operand. (See reference).

Found use of escape character instead of raw-string literals CXX-W2025
Anti-pattern
Minor
Autofix

Raw string literals are used in C++ to avoid using escape characters. They can be used to express all types of string literals. A raw string literal enables you to express a string without having to perform extra construction or conversion steps. For example, if you want to express a string that contains a backslash, you can use a raw string literal to avoid having to escape the backslash.

Using copy-swap trick instead of shrink_to_fit() on std::vector CXX-W2027
Anti-pattern
Minor

shrink_to_fit() and the copy-swap trick are two ways to reduce the capacity of a std::vector container in C++.

Found use of deprecated C++ headers CXX-W2030
Anti-pattern
Minor
Autofix

The use of deprecated C++ headers such as signal.h and assert.h is considered an antipattern, as they are not part of the C++ standard library anymore and have been replaced by their C++ equivalents.

Found use of deprecated ios_base aliases CXX-W2031
Anti-pattern
Minor

The use of deprecated ios_base aliases such as ios_base::open_mode and ios_base::seek_dir is considered an antipattern because they have been replaced by their non-deprecated equivalents.

Found use of deprecated std::random_shuffle type CXX-W2032
Anti-pattern
Minor
Autofix

std::random_shuffle is deprecated because it uses an unspecified RNG (Random Number Generator) which can lead to unpredictable behavior. The iterator-only version of std::random_shuffle usually depends on std::rand, which is now also discussed for deprecation.

Found redundant use of constructor on return, instead use the braced initializer list CXX-W2033
Anti-pattern
Minor

The use of constructor on return is considered redundant as the type has already been provided by the return type, and writing the constructor again is of no meaningful value to the program.

Found use numeric type as boolean CXX-W2034
Anti-pattern
Minor

Using numeric types as boolean is considered antipattern, because it can lead to unexpected behavior. For example, if you use an integer type as a boolean type in C++, then any non-zero value will be

Found redundant cloned branches CXX-W2041
Anti-pattern
Minor

Found repeated branches in if/else statements, consecutive repeated branches in switch statements, and identical true and false branches in conditional operators.

In the bad practice example, the then and else branches are identical, which can be considered redundant. The code can be simplified by removing the conditional statement and executing the shared code unconditionally.

However, if this is the intended behavior, there is no reason to use a conditional statement in the first place.