This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Relax the way misc-move-const-arg treats trivially copyable types
ClosedPublic

Authored by oleg on Nov 9 2017, 12:28 PM.

Details

Summary

[clang-tidy] Relax misc-move-const-arg checks w.r.t. the trivially copyable types

  • it's OK to move() these as they may stop being trivially copyable in the future + this applies to user-defined types + yet that loses the checks for scalars + added an option to control this feature + added a test

Diff Detail

Repository
rL LLVM

Event Timeline

oleg created this revision.Nov 9 2017, 12:28 PM

What problem are you trying to solve with this relaxation?

test/clang-tidy/misc-move-const-arg.cpp
26

I'm not keen on these regressions and would prefer to keep these tests working.

60–61

What's with the preceding x in xCHECK-MESSAGES and xCHECK-FIXES? It seems this is not being properly checked (the 'int' in the message would be failing, otherwise).

oleg added inline comments.Nov 15 2017, 10:57 AM
test/clang-tidy/misc-move-const-arg.cpp
60–61

That was meant to be removed. I.E. the code generates no errors now as that is the case I am trying to solve.

aaron.ballman added inline comments.Nov 15 2017, 11:26 AM
test/clang-tidy/misc-move-const-arg.cpp
60–61

Okay, that's what was causing my confusion then. :-)

I'm of two minds about this change. On the one hand, you're absolutely correct that it should be harmless to move trivially copyable types (including scalars). However, there's also utility in telling users they've written useless code that doesn't do anything.

I think this should be configured as an option that is disabled by default (so this code continues to warn), because the purpose to this check is to point out situations where the user expects a move to have impact and it's not.

oleg added inline comments.Nov 15 2017, 12:22 PM
test/clang-tidy/misc-move-const-arg.cpp
60–61

Well, there are certainly two ways of looking at the existing warnings:
a) This type is trivially copyable, so a warning is useful, so let's remove the "move" as it's a copy any way. Or,
b) This type is trivially copyable today, yet that's just an implementation detail of that particular type, so I'll keep the "move" and silence the warning globally.

Obviously there is value in finding sites for (a), yet, I think, the production case would be (b).

So, if you think that both flows are required, I'll thread an option through the code. Do we have an existing way of customizing a checker? Or would I have to create a less pedantic checker?

aaron.ballman added inline comments.Nov 15 2017, 1:03 PM
test/clang-tidy/misc-move-const-arg.cpp
60–61

Obviously there is value in finding sites for (a), yet, I think, the production case would be (b).

The documentation for the check specifically says: "The check warns... if std::move() is called with an argument of a trivially-copyable type, ...", so this seems like it was a design consideration made from the start. Given that the utility of the test is in finding cases where the move is a noop, I think the default should be (a), but I see the utility in providing an option for (b) so that you don't have to silence the diagnostic globally.

So, if you think that both flows are required, I'll thread an option through the code. Do we have an existing way of customizing a checker? Or would I have to create a less pedantic checker?

Check out MisplacedWideningCastCheck for an example of how to thread an option around. You should also modify the documentation to call out the new option (usually with an example) and test the option in a test file.

One nice thing about this option is: once it's provided, I'm no longer worried about the behavior of scalar types changing when the user elects to use the option.

oleg updated this revision to Diff 123216.Nov 16 2017, 11:09 AM
oleg edited the summary of this revision. (Show Details)

Reworked the code so that it's controlled by a user-visible option.

aaron.ballman accepted this revision.Nov 18 2017, 11:45 AM

LGTM aside from some small nits.

docs/clang-tidy/checks/misc-move-const-arg.rst
36

of the trivially -> of trivially

test/clang-tidy/misc-move-const-arg-trivially-copyable.cpp
43

This should spell out the full diagnostic text because it's the first instance of that diagnostic in the file (subsequent instances of that text can be shortened).

This revision is now accepted and ready to land.Nov 18 2017, 11:45 AM
oleg updated this revision to Diff 123471.Nov 18 2017, 12:44 PM
oleg marked 2 inline comments as done.

Thank you for the review, Aaron!

What's the lifecycle of these patches? Could you commit it on my behalf please?

In D39863#929429, @oleg wrote:

Thank you for the review, Aaron!

What's the lifecycle of these patches? Could you commit it on my behalf please?

Normally I could commit for you, but I'm currently traveling and don't have the ability to watch the bots to see if they break for some reason.

However, if you are planning to continue development in the community, you could request commit access to commit yourself: https://llvm.org/docs/DeveloperPolicy.html#obtaining-commit-access

Once you commit the patch, you should make sure you sit in the IRC channel (#llvm on OFTC) to see if any bots complain about the commit (most of the bots send email on a breakage, but a few do not).

oleg added a comment.Nov 18 2017, 5:04 PM

I've just rebased the patch over the top of LLVM/Clang/tools-extra master branches and tests pass: make check-clang-tools.

Could one of the reviewers commit this please?

In D39863#929572, @oleg wrote:

I've just rebased the patch over the top of LLVM/Clang/tools-extra master branches and tests pass: make check-clang-tools.

Could one of the reviewers commit this please?

I can do it on Tue/Wed when I get back in town if no one gets to it before me.

oleg added a comment.Nov 27 2017, 1:29 PM

Aaron, gentle ping...

aaron.ballman closed this revision.Nov 27 2017, 3:00 PM

Thank you for the ping, this fell off my radar (sorry about the delay in committing it) -- I've commit in r319111.