-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
michaelnebel
merged 5 commits into
github:main
from
michaelnebel:csharp/useless-if-statement
Mar 6, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a9d45a2
C#: Add some tests for cs/useless-if-statement.
michaelnebel 361bdfa
C#: Add a testcase with an empty if statement containing a comment.
michaelnebel 35fbaf4
C#: Do flag empty if statements if there is a comment in cs/useless-i…
michaelnebel dd7d5d0
C#: Update test expected output.
michaelnebel c73eeec
C#: Add change note.
michaelnebel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
csharp/ql/src/change-notes/2025-03-05-useless-if-statement.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* Don't consider an if-statement to be *useless* in `cs/useless-if-statement` if there is at least a comment. |
34 changes: 34 additions & 0 deletions
34
csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
|
||
class FutileConditionalTest | ||
{ | ||
|
||
public void M(string s) | ||
{ | ||
if (s.Length > 0) ; // $ Alert | ||
|
||
if (s.Length > 1) | ||
{ | ||
} // $ Alert | ||
|
||
if (s.Length > 2) // GOOD: because of else-branch | ||
{ | ||
} | ||
else | ||
{ | ||
Console.WriteLine("hello"); | ||
} | ||
|
||
if (s.Length > 3) | ||
{ | ||
} | ||
else | ||
{ | ||
} // $ Alert | ||
|
||
if (s.Length > 4) | ||
{ | ||
// GOOD: Because of the comment. | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
| FutileConditional.cs:8:9:8:27 | if (...) ... | If-statement with an empty then-branch and no else-branch. | | ||
| FutileConditional.cs:10:9:12:9 | if (...) ... | If-statement with an empty then-branch and no else-branch. | | ||
| FutileConditional.cs:22:9:27:9 | if (...) ... | If-statement with an empty then-branch and no else-branch. | |
2 changes: 2 additions & 0 deletions
2
csharp/ql/test/query-tests/Useless Code/FutileConditional/FutileConditional.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
query: Useless code/FutileConditional.ql | ||
postprocess: utils/test/InlineExpectationsTestQuery.ql |
2 changes: 2 additions & 0 deletions
2
csharp/ql/test/query-tests/Useless Code/FutileConditional/options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
semmle-extractor-options: /nostdlib /noconfig | ||
semmle-extractor-options: --load-sources-from-project:${testdir}/../../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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).