Fix for Missed opportunity to use Where - #1
Merged
Merged
Conversation
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: Alcheri <barry.suridge@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the C# Greeter plugin’s LoadTargets() logic to satisfy a CodeQL recommendation by moving per-item validation from inside the loop into the enumerable pipeline.
Changes:
- Refactors
LoadTargets()to iterate only valid targets via.Where(...)rather than guarding with anifinside theforeach. - Adds an explicit
using System.Linq;directive to support LINQ extension usage (though the project already enables implicit usings).
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+1
to
3
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Text.Json; |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
To fix this, move the validation predicate from inside the loop into the sequence itself using
.Where(...), so theforeachiterates only valid targets. This preserves behavior while matching the CodeQL rule intent.In
example.csharp.greeter/GreeterPlugin.cs, update theLoadTargets()method around lines 124–131:foreachsource fromsettings?.Targets ?? []to(settings?.Targets ?? []).Where(...).ifguard and keep only the assignment in the loop body.using System.Linq;at the top of the file (if not already present), sinceWhereis a LINQ extension method.Suggested fixes powered by Copilot Autofix. Review carefully before merging.