Skip to content

C++: New Query cpp/comma-before-misleading-indentation#10550

Merged
d10c merged 33 commits into
github:mainfrom
d10c:cpp/comma-before-misleading-indentation
Oct 12, 2022
Merged

C++: New Query cpp/comma-before-misleading-indentation#10550
d10c merged 33 commits into
github:mainfrom
d10c:cpp/comma-before-misleading-indentation

Conversation

@d10c

@d10c d10c commented Sep 23, 2022

Copy link
Copy Markdown
Contributor
  • C++: Initial commit of cpp/comma-before-missing-indentation
  • C++: Initial cpp/comma-before-misleading-indentation

@aibaars

aibaars commented Sep 23, 2022

Copy link
Copy Markdown
Contributor

@d10c Could you test this query against git/git to see if there are any false positives. They use the comma operator quite a bit, but mostly in for loop headers.

@MathiasVP

Copy link
Copy Markdown
Contributor

@d10c Could you test this query against git/git to see if there are any false positives. They use the comma operator quite a bit, but mostly in for loop headers.

git/git is part of the C/C++ DCA suite, so I definitely expect that we'll investigate results on that project before we merge this PR.

Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql Outdated
Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql Outdated
Arguably warning, not just recommendation; it may be a logic error.

TODO: What CWE/CVEs should I tag this with?
@d10c d10c marked this pull request as ready for review September 29, 2022 22:28
@d10c d10c requested a review from a team as a code owner September 29, 2022 22:28
@d10c d10c added the ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. label Sep 29, 2022

@MathiasVP MathiasVP left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks super nice! I really appreciate the amount of testing that has gone into this PR. We should be sure to start a DCA run for this before we merge it (and we might as well do that now while we're waiting for a Docs review).

Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql Outdated
Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql Outdated
Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql Outdated
Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.qhelp Outdated
d10c and others added 4 commits September 30, 2022 12:28
Co-authored-by: Mathias Vorreiter Pedersen <mathiasvp@github.com>
This has a comparable but different set of FPs as the previous version.
But arguably it's an improvement.
@github-actions

github-actions Bot commented Oct 4, 2022

Copy link
Copy Markdown
Contributor

QHelp previews:

cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.qhelp

Comma before misleading indentation

If the expression after the comma operator starts at an earlier column than the expression before the comma, then this suspicious indentation possibly indicates a logic error, caused by a typo that may escape visual inspection.

WARNING: This query has medium precision because CodeQL currently does not distinguish between tabs and spaces in whitespace. If a file contains mixed tabs and spaces, alerts may highlight code that is correctly indented for one value of tab size but not for other tab sizes.

Recommendation

To ensure that your code is easy to read and review, use standard indentation around the comma operator. Always begin the right-hand-side operand at the same level of indentation (column number) as the left-hand-side operand. This makes it easier for other developers to see the intended behavior of your code.

Use whitespace consistently to communicate your coding intentions. Where possible, avoid mixing tabs and spaces within a file. If you need to mix them, use them consistently.

Example

This example shows three different ways of writing the same code. The first example contains a comma instead of a semicolon which means that the final line is part of the if statement, even though the indentation suggests that it is intended to be separate. The second example looks different but is functionally the same as the first example. It is more likely that the developer intended to write the third example.

/*
 * In this example, the developer intended to use a semicolon but accidentally used a comma:
 */

enum privileges entitlements = NONE;

if (is_admin)
    entitlements = FULL, // BAD

restrict_privileges(entitlements);

/*
 * The use of a comma means that the first example is equivalent to this second example:
 */

enum privileges entitlements = NONE;

if (is_admin) {
    entitlements = FULL;
    restrict_privileges(entitlements);
}

/*
 * The indentation of the first example suggests that the developer probably intended the following code:
 */

enum privileges entitlements = NONE;

if (is_admin)
    entitlements = FULL; // GOOD

restrict_privileges(entitlements);

References

MathiasVP
MathiasVP previously approved these changes Oct 5, 2022

@MathiasVP MathiasVP left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code LGTM! Let's merge this once the Docs review is in 🎉.

@d10c d10c added ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. and removed ready-for-doc-review This PR requires and is ready for review from the GitHub docs team. labels Oct 10, 2022

@felicitymay felicitymay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@d10c - many thanks for including query help with your new query 💖

I've added a few questions and lots of suggestions for bringing the style into line with other queries. It's possible that I may have misunderstood the meaning of some parts, so please do check and get back to me if we need to discuss anything.

Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.qhelp Outdated
Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.qhelp Outdated
Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.qhelp Outdated
Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.qhelp Outdated
Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.ql Outdated
not isParenthesized(ce) and
leftLoc.getEndLine() < rightLoc.getStartLine() and
leftLoc.getStartColumn() > rightLoc.getStartColumn()
select right, "The indentation level may be misleading (for some tab sizes)."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there any discussion over putting "for some tab sizes)" in parentheses? I'm not sure that they are needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not necessary. But tab size is not relevant for all instances of this error, so I thought it might be odd to see tab size mentioned in an alert for code that does not use tabs for indentation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the parentheses in a later commit.

Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.cpp Outdated
Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.cpp Outdated
Comment thread cpp/ql/src/Best Practices/Likely Errors/CommaBeforeMisleadingIndentation.cpp Outdated
Co-authored-by: Felicity Chapman <felicitymay@github.com>
Co-authored-by: Geoffrey White <40627776+geoffw0@users.noreply.github.com>
@d10c

d10c commented Oct 12, 2022

Copy link
Copy Markdown
Contributor Author

Thanks for the Doc review @felicitymay! I've made changes based on your suggestions. This PR is now ready for a final review/approval.

@felicitymay felicitymay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@d10c - many thanks for the updates, and for finding some references ✨

This looks ready to merge from our side.

@d10c d10c merged commit 7b90ba6 into github:main Oct 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ documentation ready-for-doc-review This PR requires and is ready for review from the GitHub docs team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants