libelektra icon indicating copy to clipboard operation
libelektra copied to clipboard

Reformatting: avoid one-line if statements

Open lawli3t opened this issue 3 years ago • 6 comments

Currently, a lot of if statements - especially when checking for null pointers - are condensed into a single line. This is a problem for the coverage report, because the coverage only checks whether the expression has been evaluated. It is impossible to tell whether a return has been covered without looking at the tests itself. This change makes no functional difference, but it will be easier to parse the coverage output.

Examples:

  • https://github.com/ElektraInitiative/libelektra/blob/master/src/libs/elektra/key.c#L335
  • https://github.com/ElektraInitiative/libelektra/blob/master/src/libs/elektra/keyname.c#L398
  • basically any function that checks for null pointers / uses test_bit (a lot)

lawli3t avatar Mar 28 '21 16:03 lawli3t

I agree that the style is a bit odd and it sounds reasonable to me to reformat ifs to never be in a single line. With the code reformatter this should be easy to do. In .clang-format probably only AllowShortIfStatementsOnASingleLine needs to be set to false.

markus2330 avatar Mar 29 '21 13:03 markus2330

I will look into opening a MR for this.

lawli3t avatar Mar 31 '21 20:03 lawli3t

Btw. if we do not allow one-line ifs, we should also make a block around every if.

markus2330 avatar Apr 01 '21 16:04 markus2330

Yes I would have certainly done that, i dislike if-statements without blocks anyway - but thats just a personal preference i guess.

lawli3t avatar Apr 04 '21 00:04 lawli3t

Actually, there are stories about security problems introduced because of if-statements without blocks.

markus2330 avatar Apr 04 '21 04:04 markus2330

Intriguing, I've never heard of those before. But thats yet another reason for this change.

lawli3t avatar Apr 05 '21 22:04 lawli3t

@markus2330 This issue seems like a good candidate for triage work. Find and list all places (files + line-numbers) where one-line if-statements are still present.

flo91 avatar Oct 05 '22 16:10 flo91

Actually this issue is very simple to fix... There is a clang-tidy check that checks whether all ifs use braces. It can also be automatically fixed with

clang-tidy /path/to/file.c -fix -checks="readability-braces-around-statements"

After that just reformat with clang-format to correctly position the braces.


A better issue for FLOSS would be creating a .clang-tidy config file and setting up clang-tidy to run in the CI (similar to formatting).

kodebach avatar Oct 05 '22 17:10 kodebach

Yes, triage&fix is probably easiest with this clang-tidy, thanks for the tip!

A better issue

Its not better, it is a different (or if you insist: more general) issue. I created #4517.

markus2330 avatar Oct 07 '22 09:10 markus2330

I mark this stale as it did not have any activity for one year. I'll close it in two weeks if no further activity occurs. If you want it to be alive again, ping by writing a message here or create a new issue with the remainder of this issue. Thank you for your contributions :sparkling_heart:

github-actions[bot] avatar Jan 09 '24 01:01 github-actions[bot]

I closed this now because it has been inactive for more than one year. If I closed it by mistake, please do not hesitate to reopen it or create a new issue with the remainder of this issue. Thank you for your contributions :sparkling_heart:

github-actions[bot] avatar Jan 23 '24 01:01 github-actions[bot]