Skip to content

Commit b413181

Browse files
authored
path.cpp: fixed -Wuseless-cast GCC warning with C++17 (#7771)
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<char*>(buf.data()), buf.size(), fp); | ```
1 parent f8e8765 commit b413181

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/path.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,15 @@ static bool hasEmacsCppMarker(const char* path)
242242
return false;
243243
std::string buf(128, '\0');
244244
{
245+
#if __cplusplus >= 201703L
246+
// C++17 provides an overload with non-const data()
247+
#define CONST_CAST(x) (x)
248+
#else
249+
#define CONST_CAST(x) const_cast<char*>(x)
250+
#endif
245251
// TODO: read the whole first line only
246-
const char * const res = fgets(const_cast<char*>(buf.data()), buf.size(), fp);
252+
const char * const res = fgets(CONST_CAST(buf.data()), buf.size(), fp);
253+
#undef CONST_CAST
247254
fclose(fp);
248255
fp = nullptr;
249256
if (!res)

0 commit comments

Comments
 (0)