Skip to content

S3241 FP: Extension methods following fluent pattern should not be flagged #9813

Description

@SonnyRR

Description

Rule S3241 ("Change return type to 'void'; not a single caller uses the returned value") produces a false positive for private extension methods that return their this parameter type, following the standard Fluent/convention API builder pattern.

Reproducer

public static class ServiceCollectionExtensions
{
    private static IServiceCollection AddFoo(this IServiceCollection services)
    {
        return services;
    }
}

Expected

No warning- the method follows the well-known fluent builder pattern (returning the this type).

Actual

warning S3241: Change return type to 'void'; not a single caller uses the returned value.

Analysis

The rule should suppress this diagnostic when:

  1. The method is an extension method (has this parameter), AND
  2. The return type matches the this parameter type

These methods follow the same convention as all ASP.NET Core IServiceCollection / IApplicationBuilder extensions. Changing to void would break the fluent chaining pattern and prevent callers from composing calls inline. The fact that no current callers chain off the return value is by design, the return type exists for consistency, future use, and to follow the ecosystem convention.

Product and Version

10.27.0.140913

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions