Skip to content

Fix CodeQL warnings: LINQ optimizations and resource disposal#86

Merged
Malcolmnixon merged 5 commits intomainfrom
copilot/fix-codeql-findings
Jan 27, 2026
Merged

Fix CodeQL warnings: LINQ optimizations and resource disposal#86
Malcolmnixon merged 5 commits intomainfrom
copilot/fix-codeql-findings

Conversation

Copy link
Contributor

Copilot AI commented Jan 27, 2026

Pull Request

Description

Resolves 18 CodeQL warnings: 7 LINQ inefficiencies in source code and 11 resource disposal issues in tests.

LINQ Optimizations (Source)

Replaced imperative loops with declarative LINQ where CodeQL flagged implicit filtering/mapping:

  • FindRequirement: foreach.FirstOrDefault() for search-and-return pattern
  • AllRequirementTestsPassed: foreach with early return → .Select().All()
  • GetRequirementTestStats: foreach.Select() for mapping test names to results
  • CollectTestNames: nested loops → .SelectMany() and .Select()
  • CollectAllTests: foreach.Select().Where() for mapping and filtering child requirements

Resource Disposal (Tests)

All StringWriter instances now properly disposed:

// Before: leaked resources
var output = new StringWriter();
Console.SetOut(output);

// After: guaranteed disposal
using var output = new StringWriter();
Console.SetOut(output);

Replaced manual try-finally disposal in Context_Dispose_WithLogFile_ClosesLogFile with using statement for proper resource management.

Type of Change

  • Code quality improvement

Related Issues

Pre-Submission Checklist

Build and Test

  • Code builds successfully: dotnet build --configuration Release
  • All tests pass: dotnet test --configuration Release
  • Code produces zero warnings

Code Quality

  • Code formatting is correct: dotnet format --verify-no-changes
  • New code has appropriate XML documentation comments
  • Static analyzer warnings have been addressed

Quality Checks

  • Spell checker passes: cspell "**/*.{md,cs}"
  • Markdown linter passes: markdownlint "**/*.md"
  • YAML linter passes: yamllint '**/*.{yml,yaml}'

Testing

  • Added unit tests for new functionality
  • Updated existing tests if behavior changed
  • All tests follow the AAA (Arrange, Act, Assert) pattern
  • Test coverage is maintained or improved

Documentation

  • Updated README.md (if applicable)
  • Updated ARCHITECTURE.md (if applicable)
  • Added code examples for new features (if applicable)
  • Updated requirements.yaml (if applicable)

Additional Notes

CodeQL analysis now reports 0 alerts. All 109 tests pass. No functional changes - semantics preserved.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Feature]: CodeQL Cleanup</issue_title>
<issue_description>### Problem Statement

CodeQL is reporting numerous findings

Proposed Solution

Please review and fix the following findings:

src/DemaConsulting.ReqStream/TraceMatrix.cs(271): warning [cs/linq/missed-where] This foreach loop [implicitly filters its target sequence](1) - consider filtering the sequence explicitly using '.Where(...)'.  
src/DemaConsulting.ReqStream/Requirements.cs(219): warning [cs/linq/missed-select] This foreach loop immediately [maps its iteration variable to another variable](1) - consider mapping the sequence explicitly using '.Select(...)'.  
src/DemaConsulting.ReqStream/TraceMatrix.cs(225): warning [cs/linq/missed-select] This foreach loop immediately [maps its iteration variable to another variable](1) - consider mapping the sequence explicitly using '.Select(...)'.  
src/DemaConsulting.ReqStream/TraceMatrix.cs(252): warning [cs/linq/missed-select] This foreach loop immediately [maps its iteration variable to another variable](1) - consider mapping the sequence explicitly using '.Select(...)'.  
src/DemaConsulting.ReqStream/TraceMatrix.cs(280): warning [cs/linq/missed-select] This foreach loop immediately [maps its iteration variable to another variable](1) - consider mapping the sequence explicitly using '.Select(...)'.  
src/DemaConsulting.ReqStream/TraceMatrix.cs(359): warning [cs/linq/missed-select] This foreach loop immediately [maps its iteration variable to another variable](1) - consider mapping the sequence explicitly using '.Select(...)'.  
src/DemaConsulting.ReqStream/TraceMatrix.cs(465): warning [cs/linq/missed-select] This foreach loop immediately [maps its iteration variable to another variable](1) - consider mapping the sequence explicitly using '.Select(...)'.  
test/DemaConsulting.ReqStream.Tests/ProgramTests.cs(60): warning [cs/local-not-disposed] Disposable 'StringWriter' is created but not disposed.  
test/DemaConsulting.ReqStream.Tests/ProgramTests.cs(87): warning [cs/local-not-disposed] Disposable 'StringWriter' is created but not disposed.  
test/DemaConsulting.ReqStream.Tests/ContextTests.cs(394): warning [cs/local-not-disposed] Disposable 'StringWriter' is created but not disposed.  
test/DemaConsulting.ReqStream.Tests/ContextTests.cs(417): warning [cs/local-not-disposed] Disposable 'StringWriter' is created but not disposed.  
test/DemaConsulting.ReqStream.Tests/ContextTests.cs(440): warning [cs/local-not-disposed] Disposable 'StringWriter' is created but not disposed.  
test/DemaConsulting.ReqStream.Tests/ContextTests.cs(464): warning [cs/local-not-disposed] Disposable 'StringWriter' is created but not disposed.  
test/DemaConsulting.ReqStream.Tests/ProgramTests.cs(352): warning [cs/local-not-disposed] Disposable 'StringWriter' is created but not disposed.  
test/DemaConsulting.ReqStream.Tests/ProgramTests.cs(379): warning [cs/local-not-disposed] Disposable 'StringWriter' is created but not disposed.  
test/DemaConsulting.ReqStream.Tests/ContextTests.cs(508): warning [cs/local-not-disposed] Disposable 'StringWriter' is created but not disposed.  
test/DemaConsulting.ReqStream.Tests/ProgramTests.cs(487): warning [cs/local-not-disposed] Disposable 'StringWriter' is created but not disposed.  
test/DemaConsulting.ReqStream.Tests/ContextTests.cs(658): warning [cs/dispose-not-called-on-throw] Dispose missed if exception is thrown by [call to method WriteLine](1).  

Alternatives Considered

No response

Usage Examples

Benefits

Code Quality

Priority

Low - Nice to have

Willingness to Contribute

Yes, I can submit a pull request

Additional Context

No response

Checklist

  • I have searched existing issues to ensure this is not a duplicate
  • I have provided a clear description of the feature
  • I have explained why this feature would be useful</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits January 27, 2026 02:41
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix CodeQL findings in TraceMatrix and Requirements files Fix CodeQL warnings: LINQ optimizations and resource disposal Jan 27, 2026
Copilot AI requested a review from Malcolmnixon January 27, 2026 02:49
@Malcolmnixon
Copy link
Contributor

@copilot I'm still seeing the following CodeQL issues:

src/DemaConsulting.ReqStream/TraceMatrix.cs(245): warning [cs/linq/missed-select] This foreach loop immediately [maps its iteration variable to another variable](1) - consider mapping the sequence explicitly using '.Select(...)'.  
src/DemaConsulting.ReqStream/TraceMatrix.cs(343): warning [cs/linq/missed-select] This foreach loop immediately [maps its iteration variable to another variable](1) - consider mapping the sequence explicitly using '.Select(...)'.  
test/DemaConsulting.ReqStream.Tests/ContextTests.cs(656): warning [cs/missed-using-statement] This variable is manually [disposed](1) in a [finally block](2) - consider a C# using statement as a preferable resource management technique.  

Co-authored-by: Malcolmnixon <1863707+Malcolmnixon@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 27, 2026

@copilot I'm still seeing the following CodeQL issues:

src/DemaConsulting.ReqStream/TraceMatrix.cs(245): warning [cs/linq/missed-select] This foreach loop immediately [maps its iteration variabl...

Fixed all three remaining CodeQL issues in commit f31dca9:

  1. TraceMatrix.cs:245 - Used .Select() and .Where() to map and filter child requirements
  2. TraceMatrix.cs:343 - Used .Select() to map test names to results
  3. ContextTests.cs:656 - Replaced try-finally with using statement for resource management

All tests pass and build completes with zero warnings.

@Malcolmnixon Malcolmnixon marked this pull request as ready for review January 27, 2026 03:11
@Malcolmnixon Malcolmnixon merged commit bdb2796 into main Jan 27, 2026
11 checks passed
@Malcolmnixon Malcolmnixon deleted the copilot/fix-codeql-findings branch January 27, 2026 03:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: CodeQL Cleanup

2 participants