Issue 123198
Summary False positive in bugprone-string-constructor
Labels new issue
Assignees
Reporter JVApen
    ````
#include <string>

void f(std::string str)
{
          // Find the substring "FAMILY:" (copied from old code so still using C-style Char pointers)
      const char *ptr = str.c_str();
 std::string copy(ptr, 0, str.size()/2);
}
````
On Compiler Explorer: https://compiler-explorer.com/z/1o86onvGG

The result:
````
[<source>:7:19: warning: constructor creating an empty string [bugprone-string-constructor]]
   7 |       std::string copy(ptr, 0, str.size()/2);
      |                   ^
1 warning generated.
````

In this example, the following constructor of std::string should be called:
````
template< class StringViewLike >
basic_string( const StringViewLike& t,
              size_type pos, size_type count,
 const Allocator& alloc = Allocator() );
````

Since we provide a valid pointer and a valid non-0 count, the string isn't empty by default.
As such, the warning should not be given.

_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to