It is not useful to keep the statement around and can lead to compiler
warnings when -Wall (-Wunused-variable specifically) turned on.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
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.
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? |
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. |
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?