From b306c51b344f81a87384d10d49c889feb87ec083 Mon Sep 17 00:00:00 2001 From: firewave Date: Wed, 13 Aug 2025 14:50:27 +0200 Subject: [PATCH] path.cpp: fixed `-Wuseless-cast` GCC warning with C++17 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit building with the GUI enabled will implicitly switch the standard to C++17. ``` /home/runner/work/cppcheck/cppcheck/lib/path.cpp: In function ‘bool hasEmacsCppMarker(const char*)’: /home/runner/work/cppcheck/cppcheck/lib/path.cpp:246:40: error: useless cast to type ‘char*’ [-Werror=useless-cast] 246 | const char * const res = fgets(const_cast(buf.data()), buf.size(), fp); | ``` --- lib/path.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/path.cpp b/lib/path.cpp index 47a1808302c..6770267cfda 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -242,8 +242,15 @@ static bool hasEmacsCppMarker(const char* path) return false; std::string buf(128, '\0'); { +#if __cplusplus >= 201703L + // C++17 provides an overload with non-const data() + #define CONST_CAST(x) (x) +#else + #define CONST_CAST(x) const_cast(x) +#endif // TODO: read the whole first line only - const char * const res = fgets(const_cast(buf.data()), buf.size(), fp); + const char * const res = fgets(CONST_CAST(buf.data()), buf.size(), fp); +#undef CONST_CAST fclose(fp); fp = nullptr; if (!res)