libelektra
libelektra copied to clipboard
Reformatting: avoid one-line if statements
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)
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.
I will look into opening a MR for this.
Btw. if we do not allow one-line ifs, we should also make a block around every if.
Yes I would have certainly done that, i dislike if-statements without blocks anyway - but thats just a personal preference i guess.
Actually, there are stories about security problems introduced because of if-statements without blocks.
Intriguing, I've never heard of those before. But thats yet another reason for this change.
@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.
Actually this issue is very simple to fix... There is a clang-tidy
check that checks whether all if
s 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).
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.
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:
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: