False report modernize-return-braced-init-list with std::string and incorrect fix is applied
The issue is reproducible with clang 17 and 18. This file
#include <string>
std::string space(size_t n) {
return std::string(n, ' ');
}
generates a warning
warning: avoid repeating the return type from the declaration; use a braced initializer list instead [modernize-return-braced-init-list]
And with auto-fix, tidy modifies it with the following diff
< return std::string(n, ' ');
---
> return {n, ' '};
Then the compilation fails:
error: non-constant-expression cannot be narrowed from type 'size_t' (aka 'unsigned long') to 'char' in initializer list [-Wc++11-narrowing]
To reproduce:
clang-tidy '--checks=modernize-return-braced-init-list' --fix str.cpp -- -std=c++17 -stdlib=libc++
@llvm/issue-subscribers-clang-tidy
Author: None (jackua22)
To reproduce:
clang-tidy '--checks=modernize-return-braced-init-list' --fix str.cpp -- -std=c++17 -stdlib=libc++
Please assign it to me.
Furthermore, fixing the compilation error according to the suggestion introduces a bug:
<source>:3:13: error: non-constant-expression cannot be narrowed from type 'size_t' (aka 'unsigned long') to 'char' in initializer list [-Wc++11-narrowing]
3 | return {n, ' '};
| ^
<source>:3:13: note: insert an explicit cast to silence this issue
3 | return {n, ' '};
| ^
| static_cast<char>( )
Related: https://github.com/llvm/llvm-project/issues/39728