Skip to content

Commit 9287d3f

Browse files
authored
enabled and fixed -Wuseless-cast GCC warnings (#6930)
1 parent be6ec8f commit 9287d3f

11 files changed

+16
-9
lines changed

cli/stacktrace.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void print_stacktrace(FILE* output, int start_idx, bool demangling, int maxdepth
6565
const char * const secondBracketAddress = strchr(firstBracketAddress, ']');
6666
const char * const beginAddress = firstBracketAddress+3;
6767
const int addressLen = int(secondBracketAddress-beginAddress);
68-
const int padLen = int(ADDRESSDISPLAYLENGTH-addressLen);
68+
const int padLen = (ADDRESSDISPLAYLENGTH-addressLen);
6969
if (demangling && firstBracketName) {
7070
const char * const plus = strchr(firstBracketName, '+');
7171
if (plus && (plus>(firstBracketName+1))) {

cmake/compileroptions.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
6868
add_compile_options(-Wno-maybe-uninitialized) # there are some false positives
6969
add_compile_options(-Wsuggest-attribute=noreturn)
7070
add_compile_options(-Wno-shadow) # whenever a local variable or type declaration shadows another one
71+
add_compile_options_safe(-Wuseless-cast)
7172
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
7273
if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 14 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 14)
7374
# TODO: verify this regression still exists in clang-15

gui/checkstatistics.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,5 @@ QStringList CheckStatistics::getTools() const
113113
for (const QString& tool: mPerformance.keys()) ret.insert(tool);
114114
for (const QString& tool: mPortability.keys()) ret.insert(tool);
115115
for (const QString& tool: mError.keys()) ret.insert(tool);
116-
return QStringList(ret.values());
116+
return ret.values();
117117
}

gui/common.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ QString toFilterString(const QMap<QString,QString>& filters, bool addAllSupporte
5656

5757
if (addAllSupported) {
5858
entries << QCoreApplication::translate("toFilterString", "All supported files (%1)")
59-
.arg(QStringList(filters.values()).join(" "));
59+
.arg(filters.values().join(" "));
6060
}
6161

6262
if (addAll) {

gui/librarydialog.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ QString LibraryDialog::getArgText(const CppcheckLibraryData::Function::Arg &arg)
351351
s += "\n not uninit: " + QString(bool_to_string(arg.notuninit));
352352
s += "\n format string: " + QString(bool_to_string(arg.formatstr));
353353
s += "\n strz: " + QString(bool_to_string(arg.strz));
354-
s += "\n valid: " + QString(arg.valid.isEmpty() ? "any" : arg.valid);
354+
s += "\n valid: " + (arg.valid.isEmpty() ? "any" : arg.valid);
355355
for (const CppcheckLibraryData::Function::Arg::MinSize &minsize : arg.minsizes) {
356356
s += "\n minsize: " + minsize.type + " " + minsize.arg + " " + minsize.arg2;
357357
}

gui/mainwindow.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2018,7 +2018,7 @@ void MainWindow::editProjectFile()
20182018
if (!mProjectFile) {
20192019
QMessageBox msg(QMessageBox::Critical,
20202020
tr("Cppcheck"),
2021-
QString(tr("No project file loaded")),
2021+
tr("No project file loaded"),
20222022
QMessageBox::Ok,
20232023
this);
20242024
msg.exec();

lib/checkclass.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,7 @@ void CheckClass::checkConst()
21892189
std::string classname = scope->className;
21902190
const Scope *nest = scope->nestedIn;
21912191
while (nest && nest->type != Scope::eGlobal) {
2192-
classname = std::string(nest->className + "::" + classname);
2192+
classname = nest->className + "::" + classname;
21932193
nest = nest->nestedIn;
21942194
}
21952195

lib/checkstl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ void CheckStl::outOfBoundsIndexExpression()
340340
void CheckStl::outOfBoundsIndexExpressionError(const Token *tok, const Token *index)
341341
{
342342
const std::string varname = tok ? tok->str() : std::string("var");
343-
const std::string i = index ? index->expressionString() : std::string(varname + ".size()");
343+
const std::string i = index ? index->expressionString() : (varname + ".size()");
344344

345345
std::string errmsg = "Out of bounds access of $symbol, index '" + i + "' is out of bounds.";
346346

lib/checkuninitvar.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
467467
conditionAlwaysTrueOrFalse(tok->next()->astOperand2(), variableValue, &alwaysTrue, &alwaysFalse);
468468

469469
// initialization / usage in condition..
470-
if (!alwaysTrue && checkIfForWhileHead(tok->next(), var, suppressErrors, bool(number_of_if == 0), *alloc, membervar))
470+
if (!alwaysTrue && checkIfForWhileHead(tok->next(), var, suppressErrors, (number_of_if == 0), *alloc, membervar))
471471
return true;
472472

473473
// checking if a not-zero variable is zero => bail out
@@ -643,7 +643,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
643643
bool initcond = false;
644644
if (!suppressErrors) {
645645
const Token *startCond = forwhile ? tok->next() : tok->linkAt(1)->tokAt(2);
646-
initcond = checkIfForWhileHead(startCond, var, false, bool(number_of_if == 0), *alloc, membervar);
646+
initcond = checkIfForWhileHead(startCond, var, false, (number_of_if == 0), *alloc, membervar);
647647
}
648648

649649
// goto "}"

lib/json.h

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "config.h"
2323

2424
SUPPRESS_WARNING_PUSH("-Wfloat-equal")
25+
SUPPRESS_WARNING_GCC_PUSH("-Wuseless-cast")
2526
SUPPRESS_WARNING_CLANG_PUSH("-Wtautological-type-limit-compare")
2627
SUPPRESS_WARNING_CLANG_PUSH("-Wextra-semi-stmt")
2728
SUPPRESS_WARNING_CLANG_PUSH("-Wzero-as-null-pointer-constant")
@@ -34,6 +35,7 @@ SUPPRESS_WARNING_CLANG_POP
3435
SUPPRESS_WARNING_CLANG_POP
3536
SUPPRESS_WARNING_CLANG_POP
3637
SUPPRESS_WARNING_CLANG_POP
38+
SUPPRESS_WARNING_GCC_POP
3739
SUPPRESS_WARNING_POP
3840

3941
#endif // jsonH

test/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ if (BUILD_TESTS)
3737
target_compile_definitions(testrunner PRIVATE CPPCHECKLIB_IMPORT SIMPLECPP_IMPORT)
3838
target_link_libraries(testrunner cppcheck-core)
3939
endif()
40+
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
41+
# (void) in ASSERT_THROW* macros might trigger this
42+
target_compile_options_safe(testrunner -Wno-useless-cast)
43+
endif()
4044
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
4145
# $ is used in dinit() designated initialization helper
4246
target_compile_options_safe(testrunner -Wno-dollar-in-identifier-extension)

0 commit comments

Comments
 (0)