C & C++

C & C++

Made by DeepSource
Using storage specifier static inside an anonymous namespace is redundant CXX-C2021
Anti-pattern
Minor
Autofix

Using the storage specifier static inside an anonymous namespace is redundant. The anonymous namespace already limits the visibility of definitions to a single translation unit. To fix the issue, simply remove the static specifier from the definitions inside the anonymous namespace. This will make the code more concise.

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.

Boolean expression can be simplified CXX-C2015
Anti-pattern
Minor

Boolean expressions involving boolean constants can be simplified to use the appropriate boolean expression directly, using DeMorgan's Theorem. This helps in improving code readability and reducing unnecessary complexity.

Using std::string::compare over equality operators CXX-C2017
Anti-pattern
Minor

Found using equality string comparison method std::string::compare instead of using == which makes the code less readable.

Found unnecessary member initialization CXX-C2026
Anti-pattern
Minor

Member variables in a class are automatically initialized to their default values when an object of the class is created. Explicitly initializing them to the same default values is redundant and can clutter the code, making it harder to read and maintain. To fix this issue, remove the unnecessary member initializations. This will make the code cleaner and more concise.

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 usage of std::bind in place of lambda expression CXX-W2064
Anti-pattern
Minor

There are concerns related to std::bind which are addressed by lambda expression as mentioned below - std::bind can make the code harder to understand and maintain. It requires additional syntax and can be less intuitive compared to lambda expressions.

Found C-style array declaration in place of std::array or std::vector CXX-W2066
Anti-pattern
Major

Arrays declared using C-style syntax can lead to problems such as buffer overflows, memory leaks, and undefined behavior. Using std::array or std::vector provides better memory management and avoids these issues. To fix this issue, replace the C-style array declaration with std::array or std::vector, depending on the requirements of the code.

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.

Misplaced array index in subscript/indexing expression CXX-C2019
Anti-pattern
Minor

Unusual array index syntax can lead to confusion and potential bugs in the code. Using the correct array index syntax X[Y] ensures that the code is

Found typedef instead of using CXX-W2029
Anti-pattern
Minor

In C++, using and typedef both perform the same task of declaring type aliases. However, using is more idiomatic as: