Repository navigation
Fix SA1130 for anonymous methods with a natural type - #4133
Merged
Merged
Conversation
…arget delegate type From bjornhellander/NewStyleCopAnalyzers#160. Adapted while cherry-picking: the tests were merged into the existing upstream test classes and use ConfigureAwait(false) and WorkItem(3985). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 5e08267ce8915d8b0deefd6c4b7417d790f0fec7)
…arameters From C# 10, an anonymous method with a parameter list has a natural type, so it can be passed for a parameter of type Delegate, object or a type parameter. * SA1130UseLambdaSyntax threw a NullReferenceException (AD0001) for such an argument, because GetDelegateParameterList assumed the parameter type is a delegate type. It now returns null for a non-delegate type, and the analyzer checks overload resolution with the anonymous method's own parameter list instead. * The SA1130 code fix removed the parameter types when the parameter's type depends on type inference (T, or Func<T, T> for a generic method), which broke the inference. It now keeps them. The Func<T, T> case also applied before C# 10. Part of #3985
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #4133 +/- ##
==========================================
- Coverage 97.39% 97.38% -0.01%
==========================================
Files 1073 1074 +1
Lines 122990 123169 +179
Branches 4173 4185 +12
==========================================
+ Hits 119781 119946 +165
- Misses 2108 2114 +6
- Partials 1101 1109 +8 🚀 New features to boost your workflow:
|
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.
Part of #4042. Follow-up to #3985 (natural type of lambdas and method groups).
From C# 10, an anonymous method with a parameter list has a natural type. That allows
var f = delegate (int x) { ... };and passing an anonymous method for aDelegate,objectorTparameter. SA1130 didn't handle these cases.Changes
var f = delegate (int x) { return x; };,object o = ...andDelegate d = ...intox => { return x; }, which has no natural type (CS8917). The fix now keeps the explicit parameter types when there's no target delegate type:(int x) => { return x; }. This is Björn Hellander's commit, cherry-picked from his fork (5e08267ce, Review c# 10 feature: natural type of lambdas and method groups bjornhellander/NewStyleCopAnalyzers#160) with him as the author. It adds a SA1101 test for method groups with a natural type.NullReferenceException(AD0001) for an anonymous method passed for aDelegate,objector type-parameter parameter.GetDelegateParameterListassumed the parameter type is a delegate type. It now returnsnullfor other types, and the analyzer checks overload resolution with the anonymous method's own parameter list instead.T, orFunc<T, T>), the fix removed the parameter types that the type argument was inferred from. It now keeps them. TheFunc<T, T>case also applied before C# 10, so that test is in the C# 6 project.Tests
SA1130CSharp10UnitTests:TestAnonymousMethodWithNaturalTypeAsync(Björn),TestAnonymousMethodAsArgumentAsync,TestAnonymousMethodAsGenericArgumentAsyncSA1130UnitTests:TestAnonymousMethodInferringTypeArgumentAsyncSA1101CSharp10UnitTests:TestMethodGroupWithNaturalTypeAsync(Björn)Known gap, not changed here
SA1130's overload check for anonymous-method arguments builds a lambda from the target delegate's parameter names and simple type names (for example
(Int32 arg) => ...). So an argument likelist.Select(delegate (int x) { return x; })is not reported: the body refers tox, which doesn't bind. This is long-standing, not specific to C# 10. Fixing it would add new SA1130 warnings for existing code, so it should be decided separately. The generic tests here name the parameterargso the analyzer reports them.