This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] performance-unnecessary-copy-initialization: Remove the complete statement when the copied variable is unused.
ClosedPublic

Authored by flx on May 10 2021, 8:46 AM.

Details

Summary

It is not useful to keep the statement around and can lead to compiler
warnings when -Wall (-Wunused-variable specifically) turned on.

Diff Detail

Event Timeline

flx created this revision.May 10 2021, 8:46 AM
flx requested review of this revision.May 10 2021, 8:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 10 2021, 8:46 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
flx updated this revision to Diff 344149.May 10 2021, 12:31 PM

Fix test case.

Hi Felix,
Can you clarify your concern over the warning? Is it the case that the warning is not present before the fix and is only triggered after the fix? I'm concerned that removing the call may have unintended side effects and I would think it might better to leave the check for usage separate. However, if the fix is causing the warning then I can understand better why you want to make this change. That said, have considered replacing the var binding with (void), rather than deleting the statement?

flx added a comment.May 14 2021, 10:28 AM

Hi Felix,
Can you clarify your concern over the warning? Is it the case that the warning is not present before the fix and is only triggered after the fix? I'm concerned that removing the call may have unintended side effects and I would think it might better to leave the check for usage separate. However, if the fix is causing the warning then I can understand better why you want to make this change. That said, have considered replacing the var binding with (void), rather than deleting the statement?

Yes, the fix of converting the copied variable to a const reference introduces a warning when -Wall (or more specifically -Wunused) is specified and the variable is not used at all. The fix converts the local variable to a const reference. Take a const reference of an object and not using it has no observable side effects I think.

Here is an example of an unused, but copied variable before the fix:

https://godbolt.org/z/a5E4fMWKj

The compiler flags are "-Werror -Wall": no warnings or errors are produced.

Now after the fix:

https://godbolt.org/z/a5E4fMWKj

For more context, the assumption the check makes is that the copy is unnecessary. If the type executes specific side-effects in its copy-constructor code the type should be excluded from the check using the AllowedTypes setting.

ymandel accepted this revision.Jun 2 2021, 7:01 AM
ymandel added inline comments.
clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-copy-initialization.cpp
541

If this line weren't here, then we'd delete line 537, in which case Ref itself becomes unused and so line 536 should be deleted as well. Do you handle this case?

This revision is now accepted and ready to land.Jun 2 2021, 7:01 AM
flx added a comment.Jun 9 2021, 12:51 PM

Thank you for the review, Yitzhak!

clang-tools-extra/test/clang-tidy/checkers/performance-unnecessary-copy-initialization.cpp
541

Good catch. This case is not handled. It would require recursively checking whether the deleted statement was the only reference to another reference which is hard and requires subtracting that decl ref from all these calculations.

I would like to to see this being a common enough issue before addressing it.

This revision was landed with ongoing or failed builds.Jun 9 2021, 12:54 PM
This revision was automatically updated to reflect the committed changes.