This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix `cppcoreguidelines-init-variables` for invalid vardecl
ClosedPublic

Authored by Sockke on Nov 24 2022, 4:19 AM.

Details

Diff Detail

Event Timeline

Sockke created this revision.Nov 24 2022, 4:19 AM
Herald added a project: Restricted Project. · View Herald Transcript
Sockke requested review of this revision.Nov 24 2022, 4:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 24 2022, 4:19 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
Eugene.Zelenko added a subscriber: Eugene.Zelenko.

Will be good idea to add test for this situation.

Sockke updated this revision to Diff 477861.Nov 24 2022, 11:34 PM

Added test.

It'll be better to expand existing init-variables.cpp test.

Sockke updated this revision to Diff 478529.Nov 29 2022, 4:52 AM

Added test in existing init-variables.cpp file.

Sockke added a comment.Dec 2 2022, 3:53 AM

Friendly ping.

Friendly ping.

Looks good in general, IMHO it would be good with a couple more comments to make the code and test easier to understand.

clang-tools-extra/clang-tidy/cppcoreguidelines/InitVariablesCheck.cpp
62

Add a comment as to why this is needed? I haven't seen this pattern often in checks.

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp
4

Add a comment explaining the purpose of introducing this error? At first sight, it's not obvious to me why this is needed.

132

Perhaps add a comment explaining that clang-tidy should not warn here, and why.

Sockke updated this revision to Diff 484210.Dec 20 2022, 3:49 AM

Added comments

Now that I think a bit better about this I wonder - does it really make sense that we increase the complexity of the check to cover for cases where code does not compile? If it fails to include a header, there's many other things that can possibly go wrong - should clang-tidy checks in general really be defensive against that? @njames93 WDYT?

Now that I think a bit better about this I wonder - does it really make sense that we increase the complexity of the check to cover for cases where code does not compile? If it fails to include a header, there's many other things that can possibly go wrong - should clang-tidy checks in general really be defensive against that? @njames93 WDYT?

It's not a hard rule that we should be defensive against clang-tidy emitting false positives due to a previous compilation error. However for simple cases, like this, where its just ensuring a declaration is valid before we try to emit diagnostic, It does make sense to handle this.
With this change the main thing would be a little less noise while trying to debug the cause of the compilation error which is always a good thing.

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/init-variables.cpp
4–13

All this is unnecessary there is a much nicer way to test this, see below

129–131

This test line is not testing the behaviour described in this patch and can be removed.

134

Just use this and the declaration will be invalid and you can test the expected behaviour

Sockke updated this revision to Diff 484765.Dec 22 2022, 1:50 AM

Improved the test file.

Improved the test file.

Just a general workflow comment, can you please mark comments as done when they are addressed, or, if you feel the comment in unjustified, explain why.

Sockke marked 6 inline comments as done.Dec 22 2022, 10:35 PM

Mark comments.

Friendly ping.

Sockke added a comment.Jan 4 2023, 1:45 AM

Friendly ping.

Hi, Could anyone please review this diff?

carlosgalvezp accepted this revision.Jan 22 2023, 7:51 AM

LGTM, thanks for the fix!

This revision is now accepted and ready to land.Jan 22 2023, 7:51 AM

@Sockke do you need help landing the patch? If so, let me know user and email for attribution :)

@Sockke ping, can you land this or should we do it for you? If so, please let us know what name and email we should use for attribution.

Sockke updated this revision to Diff 495413.Feb 7 2023, 1:11 AM

Rebased.

carlosgalvezp accepted this revision.Feb 7 2023, 1:23 AM
Sockke added a comment.Feb 7 2023, 3:36 AM

@carlosgalvezp Thanks for your review! I committed it myself.