This is an archive of the discontinued LLVM Phabricator instance.

clang-tidy: strip useless parens from `return` statements
Needs ReviewPublic

Authored by oleg.smolsky on Aug 16 2022, 11:20 AM.

Details

Summary
  • this adds a new check: readability-return-with-redundant-parens

We can find these with the AST matcher. We exclude macros.

Diff Detail

Event Timeline

oleg.smolsky created this revision.Aug 16 2022, 11:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 16 2022, 11:20 AM
oleg.smolsky requested review of this revision.Aug 16 2022, 11:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 16 2022, 11:20 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Drop the trailing \n from the main C++ file

Eugene.Zelenko set the repository for this revision to rG LLVM Github Monorepo.
Eugene.Zelenko added a subscriber: Eugene.Zelenko.

Please add documentation and mention new check in Release Notes.

The idea of this check is good, but restricting it to only return statements seems baffling. A general check that could remove useless parens would have a lot more value.

Entry in Release Notes is still missing.

clang-tools-extra/docs/clang-tidy/checks/readability/return-with-redundant-parens.rst
5

Please make same length as check name.

7

Please use double back-ticks for language constructs.

Add a release note.

Eugene.Zelenko added inline comments.Aug 16 2022, 4:19 PM
clang-tools-extra/docs/ReleaseNotes.rst
110

Please use double back-ticks for language constructs.

oleg.smolsky added a comment.EditedAug 16 2022, 4:20 PM

The idea of this check is good, but restricting it to only return statements seems baffling. A general check that could remove useless parens would have a lot more value.

Of course a more general check would be more generally useful. Yet that requires a lot more code as handling many more contexts is involved in C++.

Basically this change addresses a concrete, somewhat wide-spread silly habit that exists in the wild.

Improve mark up in the docs.

oleg.smolsky marked 3 inline comments as done.Aug 16 2022, 4:24 PM

Thanks for the quick review, Eugene!

clang-tools-extra/docs/ReleaseNotes.rst
110

Done.

clang-tools-extra/docs/clang-tidy/checks/readability/return-with-redundant-parens.rst
5

Done

7

Done.

The idea of this check is good, but restricting it to only return statements seems baffling. A general check that could remove useless parens would have a lot more value.

Of course a more general check would be more generally useful. Yet that requires a lot more code as handling many more contexts in involved in C++.

Basically this change addresses a concrete, somewhat wide-spread silly habit that exists in the wild.

I recently observed in the wild patterns like:

std::vector<std::string> data;

for (std::vector<std::string>::iterator it = data.begin(); it != data.end(); ++it)
  std::cout << (*it) << std::endl;
oleg.smolsky marked 3 inline comments as done.Aug 16 2022, 4:33 PM

The idea of this check is good, but restricting it to only return statements seems baffling. A general check that could remove useless parens would have a lot more value.

Of course a more general check would be more generally useful. Yet that requires a lot more code as handling many more contexts in involved in C++.

Basically this change addresses a concrete, somewhat wide-spread silly habit that exists in the wild.

I recently observed in the wild patterns like:

std::vector<std::string> data;

for (std::vector<std::string>::iterator it = data.begin(); it != data.end(); ++it)
  std::cout << (*it) << std::endl;

While I have not seen that one, I've seen something similar, with even more involved syntax:

for (auto it = data.begin(); it != data.end(); ++it)
   std::cout << (*it).something << std::endl;

people write wild syntax...

Add AST matchers for the if statements and improve the actual check implementation.

Simplify and generalize the AST matcher. We just want to find all return statements in function definitions.

LegalizeAdulthood resigned from this revision.Mar 29 2023, 8:25 AM