Skip to content

Potential fix for code scanning alert no. 4: Missed opportunity to use Where #339

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 1 commit into from
Mar 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
attributeLookup = new Lazy<Func<SyntaxNode, AttributeData?>>(() =>
{
var dict = new Dictionary<SyntaxNode, AttributeData?>();
foreach (var attributeData in cx.Compilation.Assembly.GetAttributes().Concat(cx.Compilation.Assembly.Modules.SelectMany(m => m.GetAttributes())))
foreach (var attributeData in cx.Compilation.Assembly.GetAttributes().Concat(cx.Compilation.Assembly.Modules.SelectMany(m => m.GetAttributes())).Where(attributeData => attributeData.ApplicationSyntaxReference?.GetSyntax() is SyntaxNode syntax))

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This assignment to
syntax
is useless, since its value is never read.

Copilot Autofix

AI about 1 month ago

To fix the problem, we need to remove the unnecessary assignment to the syntax variable. This can be done by simply deleting the line where the assignment occurs. This change will not affect the existing functionality of the code.

Suggested changeset 1
csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs
--- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs
+++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs
@@ -27,3 +27,3 @@
                     var dict = new Dictionary<SyntaxNode, AttributeData?>();
-                    foreach (var attributeData in cx.Compilation.Assembly.GetAttributes().Concat(cx.Compilation.Assembly.Modules.SelectMany(m => m.GetAttributes())).Where(attributeData => attributeData.ApplicationSyntaxReference?.GetSyntax() is SyntaxNode syntax))
+                    foreach (var attributeData in cx.Compilation.Assembly.GetAttributes().Concat(cx.Compilation.Assembly.Modules.SelectMany(m => m.GetAttributes())).Where(attributeData => attributeData.ApplicationSyntaxReference?.GetSyntax() is SyntaxNode))
                     {
EOF
@@ -27,3 +27,3 @@
var dict = new Dictionary<SyntaxNode, AttributeData?>();
foreach (var attributeData in cx.Compilation.Assembly.GetAttributes().Concat(cx.Compilation.Assembly.Modules.SelectMany(m => m.GetAttributes())).Where(attributeData => attributeData.ApplicationSyntaxReference?.GetSyntax() is SyntaxNode syntax))
foreach (var attributeData in cx.Compilation.Assembly.GetAttributes().Concat(cx.Compilation.Assembly.Modules.SelectMany(m => m.GetAttributes())).Where(attributeData => attributeData.ApplicationSyntaxReference?.GetSyntax() is SyntaxNode))
{
Copilot is powered by AI and may make mistakes. Always verify output.
{
if (attributeData.ApplicationSyntaxReference?.GetSyntax() is SyntaxNode syntax)
dict.Add(syntax, attributeData);
var syntax = attributeData.ApplicationSyntaxReference?.GetSyntax() as SyntaxNode;

Check warning

Code scanning / CodeQL

Cast to same type Warning

This cast is redundant because the expression already has type SyntaxNode.

Copilot Autofix

AI about 1 month ago

To fix the problem, we need to remove the redundant cast from the expression attributeData.ApplicationSyntaxReference?.GetSyntax() as SyntaxNode. This can be done by simply using the expression attributeData.ApplicationSyntaxReference?.GetSyntax() directly, as it already returns a SyntaxNode.

  • Locate the line with the redundant cast in the file csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs.
  • Remove the as SyntaxNode cast from the expression.
  • Ensure that the functionality remains unchanged.
Suggested changeset 1
csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs
--- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs
+++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs
@@ -29,3 +29,3 @@
                     {
-                        var syntax = attributeData.ApplicationSyntaxReference?.GetSyntax() as SyntaxNode;
+                        var syntax = attributeData.ApplicationSyntaxReference?.GetSyntax();
                         dict.Add(syntax, attributeData);
EOF
@@ -29,3 +29,3 @@
{
var syntax = attributeData.ApplicationSyntaxReference?.GetSyntax() as SyntaxNode;
var syntax = attributeData.ApplicationSyntaxReference?.GetSyntax();
dict.Add(syntax, attributeData);
Copilot is powered by AI and may make mistakes. Always verify output.
dict.Add(syntax, attributeData);

Check warning on line 31 in csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs

View workflow job for this annotation

GitHub Actions / stubgentest

Possible null reference argument for parameter 'key' in 'void Dictionary<SyntaxNode, AttributeData?>.Add(SyntaxNode key, AttributeData? value)'.

Check warning on line 31 in csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs

View workflow job for this annotation

GitHub Actions / stubgentest

Possible null reference argument for parameter 'key' in 'void Dictionary<SyntaxNode, AttributeData?>.Add(SyntaxNode key, AttributeData? value)'.

Check warning on line 31 in csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

Possible null reference argument for parameter 'key' in 'void Dictionary<SyntaxNode, AttributeData?>.Add(SyntaxNode key, AttributeData? value)'.

Check warning on line 31 in csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

Possible null reference argument for parameter 'key' in 'void Dictionary<SyntaxNode, AttributeData?>.Add(SyntaxNode key, AttributeData? value)'.

Check warning on line 31 in csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs

View workflow job for this annotation

GitHub Actions / unit-tests (ubuntu-latest)

Possible null reference argument for parameter 'key' in 'void Dictionary<SyntaxNode, AttributeData?>.Add(SyntaxNode key, AttributeData? value)'.

Check warning on line 31 in csharp/extractor/Semmle.Extraction.CSharp/Populators/TypeContainerVisitor.cs

View workflow job for this annotation

GitHub Actions / unit-tests (windows-2019)

Possible null reference argument for parameter 'key' in 'void Dictionary<SyntaxNode, AttributeData?>.Add(SyntaxNode key, AttributeData? value)'.
}
return dict.GetValueOrDefault;
});
Expand Down
Loading