Skip to content

False positive in bugprone-string-constructor #123198

@JVApen

Description

@JVApen
#include <string>

void f(std::string str)
{
      const char *ptr = str.c_str();
      std::string copy(ptr, 0, str.size()/2);
}

On Compiler Explorer: https://compiler-explorer.com/z/KEEPh3Kz1

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions