This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix extern fixes in readability-redundant-declaration
ClosedPublic

Authored by PiotrZSL on Mar 26 2023, 2:47 AM.

Details

Summary

Currently check does not look into LinkageSpecDecl
when removing redundant variable re-declarations.
This were leaving code in non-compiling state.
Fix patch fixes this and adds removal also of 'extern C'.

Fixes: https://github.com/llvm/llvm-project/issues/42068

Diff Detail

Event Timeline

PiotrZSL created this revision.Mar 26 2023, 2:47 AM
Herald added a project: Restricted Project. · View Herald Transcript
PiotrZSL requested review of this revision.Mar 26 2023, 2:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 26 2023, 2:47 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
Eugene.Zelenko added inline comments.
clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
84

auto could be used because type is explicitly stated in same statement.

PiotrZSL updated this revision to Diff 508411.Mar 26 2023, 6:52 AM
PiotrZSL marked an inline comment as done.

Use auto

Looks good, thanks for the fix! Do we think it's worth documenting in the Release Notes?

clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
86

This can be removed, leaving it as:

if (const auto *Extern = ... && !Extern->hasBraces())
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-declaration.cpp
126

Typo: dummy

I don't quite see why these are needed to test the example in PR42068?

PiotrZSL added inline comments.Apr 8 2023, 8:10 AM
clang-tools-extra/test/clang-tidy/checkers/readability/redundant-declaration.cpp
126

I wasn't sure about getLocForEndOfToken, so just wanted to add some tokens before and after, to be 100% sure that they wont be removed.

PiotrZSL added inline comments.Apr 8 2023, 9:22 AM
clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
86

If you mean:

 if (const auto *Extern =
              Result.Nodes.getNodeAs<LinkageSpecDecl>("extern");
          !Extern->hasBraces())
`

then no, it cannot be like this because Extern can be null.

PiotrZSL updated this revision to Diff 511901.Apr 8 2023, 9:37 AM

Added release notes, fixed typo.

carlosgalvezp accepted this revision.Apr 8 2023, 9:54 AM

Looks great, thanks!

clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp
86

You are right, this can only be done when there's no other conditions in the if!

This revision is now accepted and ready to land.Apr 8 2023, 9:54 AM
PiotrZSL marked 2 inline comments as done.Apr 8 2023, 9:59 AM