create a check that replaces 'std::unique_ptr<type>(new type(args...))' with 'std::make_unique<type>(args...)'. It was on the list of "Ideas for new Tools". It needs to be tested more carefully, but first I wanted to know if you think it is worth the effort.
Details
Diff Detail
Event Timeline
Remove 'hasCanonicalType'.
clang-tidy/modernize/MakeUniqueCheck.cpp | ||
---|---|---|
26–28 | Maybe using macros or something like that. As I said, it needs to be more carefully tested. |
clang-tidy/modernize/MakeUniqueCheck.cpp | ||
---|---|---|
26–28 | That should not be possible for the spelling location. Generally, I'd avoid writing code that doesn't fix a breaking test. |
Thanks for the tests - question is: I would have expected us to use something like Lexer::getSourceText, which should give us the full range in the first test (do we not want to replace it?) and tell us that we don't have the type in one consecutive range in the second case.
How can Lexer::getSourceText give us the range? My problem precisely is
that I couldn't find any way to obtain the range of the constructor call
without the template arguments.
clang-tidy/modernize/MakeUniqueCheck.cpp | ||
---|---|---|
76–77 | Comment what cases this can happen in, and why we don't want to replace in those cases... |
This raises a question. Do we want to do replacements when we use an alias
for std::unique_ptr? That fact that something is an unique_ptr might be an
implementation detail that should not be exposed, but it could also happen
that the alias is there only for brevity. What do you think?
Good question. Generally, it seems to me that aliasing unique_ptr wouldn't seem useful apart from having a shortcut; unique_ptr is special in a couple of ways, which would make it particularly weird as an "implementation detail".
My take on it is that we want to replace those, until we find real use cases that warrant a more complex implementation.
apart from the comment, LG
clang-tidy/modernize/MakeUniqueCheck.cpp | ||
---|---|---|
68–71 | Please add a comment what this does (in which cases do we want to insert the type). |
How can that ever return 0 if you have matchesName("::std::unique_ptr") below?