static
inside an anonymous namespace is redundant CXX-C2021Using 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.
Fall-through in switch cases is often considered risky.
Hence consider adding an unconditional break
for each switch clause.
Default case provides switch statements with fallback, and in general is a good to have. Hence consider adding default case to switch.
*
, "
, '
, \
and /*
found in header names CXX-W1207Using special-meaning characters in header names can produce errors in parsing.
Consider cleaning up header names.
for
loop modified in body CXX-W1241Modifying 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 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.
std::string::compare
over equality operators CXX-C2017Found using equality string comparison method std::string::compare
instead of using ==
which makes the code less readable.
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.
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.
shrink_to_fit()
on std::vector
CXX-W2027shrink_to_fit()
and the copy-swap trick are two ways to reduce the capacity of a std::vector
container in C++.
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.
ios_base
aliases CXX-W2031The 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.
std::random_shuffle
type CXX-W2032std::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.
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.
std::bind
in place of lambda expression CXX-W2064There 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.
std::array
or std::vector
CXX-W2066Arrays 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.
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 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.
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
typedef
instead of using
CXX-W2029In C++, using
and typedef
both perform the same task of declaring type aliases. However, using
is more idiomatic as: