Skip to content

Add rule NUnit2057: Remove unnecessary lambda#928

Merged
manfred-brands merged 2 commits intomasterfrom
Issue927_RemoveUnnecessaryLambda
Oct 30, 2025
Merged

Add rule NUnit2057: Remove unnecessary lambda#928
manfred-brands merged 2 commits intomasterfrom
Issue927_RemoveUnnecessaryLambda

Conversation

@manfred-brands
Copy link
Copy Markdown
Member

Fixes #927

@mikkelbu mikkelbu requested a review from Copilot October 21, 2025 18:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread documentation/NUnit2057.md Outdated
Copy link
Copy Markdown
Member

@mikkelbu mikkelbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work as usual @manfred-brands. I've only a couple of comments, but I'm happy to merge this when you are

Comment thread documentation/NUnit2057.md Outdated
Comment thread src/nunit.analyzers.tests/DelegateUnnecessary/DelegateUnnecessaryAnalyzerTests.cs Outdated
@manfred-brands manfred-brands force-pushed the Issue927_RemoveUnnecessaryLambda branch from c7f8b94 to 1436360 Compare October 30, 2025 04:38
@manfred-brands manfred-brands force-pushed the Issue927_RemoveUnnecessaryLambda branch from 1436360 to 19238b8 Compare October 30, 2025 04:45
@manfred-brands manfred-brands merged commit f02fd7b into master Oct 30, 2025
6 checks passed
@manfred-brands manfred-brands deleted the Issue927_RemoveUnnecessaryLambda branch October 30, 2025 05:08
@mikkelbu mikkelbu added this to the Release 4.11.1 milestone Oct 30, 2025
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.

Add new rule for unnecessary use of lambda expressions

3 participants