[clang-tools-extra] [clang-tidy] Add a new check 'modernize-use-string-view' (PR #172170)

Zinovy Nis via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 22 10:35:21 PST 2025


irishrover wrote:

> > > ```c++
> > > std::string getArcana(const TemplateName &NNS) { 
> > > return std::string{""}; // no warning, explicit ctor call
> > > return std::string(""); // no warning
> > > }
> > > ```
> > 
> > 
> > It works even w/o options. Just wrap literal with `std::string(...)` or `std::string{}`. Pls, see the tests:
> > ```c++
> > std::string explicitConstructionEmpty() {
> >   return std::string("");
> > }
> > 
> > std::string explicitConstructionWithInitListEmpty() {
> >   return std::string{""};
> > }
> > ```
> 
> In tests they are marked as "todo" for now,
> 
> ```c++
> std::string explicitConstruction() {
>   // Cannot be std::string_view: returning address of local temporary object
>   // TODO: extract and return literal
>   return std::string("explicitConstruction");
> }
> 
> std::string explicitConstructionWithInitList() {
>   // Cannot be std::string_view: returning address of local temporary object
>   // TODO: extract and return literal
>   return std::string{"explicitConstruction"};
> }
> ```
> 
> When eventually have this is implemented, should become an option. Please state in docs that diagnostic could be suppressed by manually constructing string from literal.
> 
> Also, please state in docs what are current limitations of this check (ternary op, literals inside strings) so that user don't report them as FN.

Done!

https://github.com/llvm/llvm-project/pull/172170


More information about the cfe-commits mailing list