Skip to content

Fix SA1130 for anonymous methods with a natural type - #4133

Merged
AArnott merged 2 commits into
masterfrom
csharp10-sa1130-natural-type
Oct 8, 2026
Merged

AArnott merged 2 commits into
masterfrom
csharp10-sa1130-natural-type

Conversation

@AArnott

@AArnott AArnott commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

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 a Delegate, object or T parameter. SA1130 didn't handle these cases.

Changes

  1. The SA1130 code fix produced code that doesn't compile. It turned var f = delegate (int x) { return x; };, object o = ... and Delegate d = ... into x => { 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.
  2. SA1130 threw a NullReferenceException (AD0001) for an anonymous method passed for a Delegate, object or type-parameter parameter. GetDelegateParameterList assumed the parameter type is a delegate type. It now returns null for other types, and the analyzer checks overload resolution with the anonymous method's own parameter list instead.
  3. The code fix broke type inference. For a generic parameter (T, or Func<T, T>), the fix removed the parameter types that the type argument was inferred from. It now keeps them. The Func<T, T> case also applied before C# 10, so that test is in the C# 6 project.

Tests

  • SA1130CSharp10UnitTests: TestAnonymousMethodWithNaturalTypeAsync (Björn), TestAnonymousMethodAsArgumentAsync, TestAnonymousMethodAsGenericArgumentAsync
  • SA1130UnitTests: TestAnonymousMethodInferringTypeArgumentAsync
  • SA1101CSharp10UnitTests: 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 like list.Select(delegate (int x) { return x; }) is not reported: the body refers to x, 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 parameter arg so the analyzer reports them.

bjornhellander and others added 2 commits October 7, 2026 21:11
…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

codecov Bot commented Oct 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.81081% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.38%. Comparing base (53ce0bf) to head (ee869c3).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AArnott
AArnott marked this pull request as ready for review October 8, 2026 04:01
@AArnott
AArnott merged commit e774598 into master Oct 8, 2026
25 checks passed
@AArnott
AArnott deleted the csharp10-sa1130-natural-type branch October 8, 2026 14:04
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.

2 participants