#include<cstring>#include<iostream>#include<string>intmain(){std::strings="foo";// std::strlen(s.c_str(()) is inefficient and should rather// be written as s.size().std::cout<<std::strlen(s.c_str())<<std::endl;return0;}
Neither gcc -Wall, nor clang -Wall, nor cppcheck warn about
the inneficient std::strlen(s.c_str()) which should rather
be s.size().
It may seem silly to write such inneficient code, but it can happen
by accident when refactoring code replacing C string with std::string.
It would be nice if cppcheck could warn about it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Consider the following c++ code:
Neither gcc -Wall, nor clang -Wall, nor cppcheck warn about
the inneficient std::strlen(s.c_str()) which should rather
be s.size().
It may seem silly to write such inneficient code, but it can happen
by accident when refactoring code replacing C string with std::string.
It would be nice if cppcheck could warn about it.
sounds good to me. I created https://trac.cppcheck.net/ticket/9015
If you want.. feel free to work on this.