[PATCH] D69238: Fix clang-tidy readability-redundant-string-init for c++17/c++2a

Conrad Poelman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 21 00:20:30 PDT 2019


poelmanc created this revision.
poelmanc added reviewers: gribozavr, etienneb, alexfh.
Herald added subscribers: cfe-commits, mgehre, dylanmckay.
Herald added a project: clang.

`readability-redundant-string-init` was one of several clang-tidy checks documented as failing for C++17 by @gribozavr's extensive testing on 2019-05-20. (The failure mode in C++17 is that it changes `std::string Name = "";` to `std::string Name = Name;`, which actually compiles.)

I found that the `CtorExpr->getSourceRange()` for `string Name = ""` changed from spanning the `Name = ""` in C++11/14 to only spanning the `""` in C++17/2a. My fix was to add a `CtorSourceRange` function that, if CtorExpr does not already start with `Name`, searches backwards for `Name = ` and extends the SourceRange to include it. The readability-redundant-string-init.cpp and readability-redundant-string-init-msvc.cpp tests now pass for C++11/14/17/2a. (The warning for `DECL_STRING(e, "");` is reported at different character positions in 11/14 versus 17/2a, so I updated that test to accept either position.)

As always I welcome any feedback or suggestions for improvement. In particular, the fix would be far simpler if there's some other expression matcher that captures the whole `Name = ""` expression. Or, is it possible that this change in SourceRange is an unintentional difference in the parsing code? If so fixing that might make more sense.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D69238

Files:
  clang-tools-extra/clang-tidy/readability/RedundantStringInitCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init-msvc.cpp
  clang-tools-extra/test/clang-tidy/checkers/readability-redundant-string-init.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69238.225823.patch
Type: text/x-patch
Size: 5461 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191021/4db7fb90/attachment-0001.bin>


More information about the cfe-commits mailing list