Skip to content

C#: Fewer alerts in cs/useless-if-statement. #18935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 6, 2025

Conversation

michaelnebel
Copy link
Contributor

@michaelnebel michaelnebel commented Mar 5, 2025

It is not an un-common pattern to write something like

if (b) {
  // Insert some relevant debugging statements here
  // My commented out code.
}

Due to the above our query might seem a bit to noisy. In this PR we remove the alerts, if the empty if-block contains a comment.
We still catch the following examples (where the first one is the most important as it is most likely unintentional).

if (b);

if (b)
{
}

@michaelnebel
Copy link
Contributor Author

DCA looks good.

@michaelnebel michaelnebel marked this pull request as ready for review March 6, 2025 09:25
@Copilot Copilot AI review requested due to automatic review settings March 6, 2025 09:26
@michaelnebel michaelnebel requested a review from a team as a code owner March 6, 2025 09:26
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

PR Overview

This PR adjusts the behavior of the cs/useless-if-statement query to reduce alerts when an empty if-statement contains a comment. It updates test cases to reflect the new behavior and updates the documentation to clarify that if a comment is present, the statement is not flagged.

Reviewed Changes

File Description
csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.cs Adds test cases that demonstrate when alerts should and should not be raised
csharp/ql/src/change-notes/2025-03-05-useless-if-statement.md Updates the change notes to document the new behavior for commented if-blocks

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more

Comment on lines +14 to +16
if (s.Length > 2) // GOOD: because of else-branch
{
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated to your change, I find it odd to consider this okay.

Copy link
Contributor Author

@michaelnebel michaelnebel Mar 6, 2025

Choose a reason for hiding this comment

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

Yes, I agree. Maybe an idea for a quality query:
Create a new quality query which states that the condition should be inverted (we can also provide good QHelp for that - and I suspect an LLM will be good at figuring out how the fix should look like).

Copy link
Contributor Author

@michaelnebel michaelnebel Mar 6, 2025

Choose a reason for hiding this comment

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

Hmm there is already a button for that in VSCode, so it might not be relevant (but you will have to actively click that).

@michaelnebel michaelnebel merged commit 61c043f into github:main Mar 6, 2025
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants