Add rule NUnit2057: Remove unnecessary lambda#928
Merged
manfred-brands merged 2 commits intomasterfrom Oct 30, 2025
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR implements a new analyzer rule NUnit2057 that detects and removes unnecessary lambda expressions in NUnit assertions. The rule identifies cases where developers wrap method calls in lambda expressions when only the return value is needed, excluding scenarios where delegates are necessary (exception catching or delayed constraints).
Key changes:
- New analyzer NUnit2057 to detect unnecessary delegate creation in Assert.That calls
- Code fix provider to automatically remove unnecessary lambdas or invoke method groups
- Refactored common async method signature update logic into shared helper
Reviewed Changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| DelegateUnnecessaryConstants.cs | Defines diagnostic message constants for the new rule |
| DelegateUnnecessaryAnalyzer.cs | Implements analyzer logic to detect unnecessary delegates in assertions |
| AnalyzerPropertyKeys.cs | Adds IsAsync property key for tracking async operations |
| AnalyzerIdentifiers.cs | Registers NUnit2057 identifier |
| NullConstraintUsageAnalyzerTests.cs | Adds pragma warnings to suppress NUnit2057 in test cases |
| DelegateUnnecessaryCodeFixTests.cs | Test coverage for the code fix functionality |
| DelegateUnnecessaryAnalyzerTests.cs | Test coverage for the analyzer detection logic |
| UseAssertEnterMultipleScopeCodeFix.cs | Refactored to use shared helper method for async signature updates |
| CodeFixHelper.cs | Extracted common async method signature update logic into reusable helper |
| DelegateUnnecessaryCodeFix.cs | Implements code fix to remove unnecessary lambdas and invoke method groups |
| index.md | Documents the new NUnit2057 rule |
| NUnit2057.md | Detailed documentation for the rule with examples |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
mikkelbu
approved these changes
Oct 21, 2025
Member
mikkelbu
left a comment
There was a problem hiding this comment.
Great work as usual @manfred-brands. I've only a couple of comments, but I'm happy to merge this when you are
c7f8b94 to
1436360
Compare
1436360 to
19238b8
Compare
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.
Fixes #927