Skip to content

[RCS0034] Rule reports false positive on C# 12 Primary Constructors #1724

@MaksHladki

Description

@MaksHladki

Product and Version Used

Roslynator Version: 4.14.1
IDE: Visual Studio 2022
Framework: .NET 8.0

Description

The analyzer RCS0034 (Put type parameter constraint on its own line) reports a warning even when the constraint is placed on a new line, specifically when using C# 12 Primary Constructors.
It seems the analyzer does not account for the primary constructor parameter list appearing between the class declaration and the where clause.

Steps to Reproduce

  1. Create a class using C# 12 Primary Constructors with a generic type constraint.
public class MixedConstraintService<TKey, TValue>(TKey key, TValue value) where TKey : notnull where TValue : new()
{
    public TKey Key { get; } = key;
    public TValue Value { get; } = value;
}
  1. Format the code so the where clause is on its own line (as required by RCS0034).
public class MixedConstraintService<TKey, TValue>(TKey key, TValue value)
    where TKey : notnull
    where TValue : new()
{
    public TKey Key { get; } = key;
    public TValue Value { get; } = value;
}

Expected behavior

The analyzer should not report a warning because the where constraint is already on its own line.

Actual behavior

The analyzer reports: warning RCS0034: Put type parameter constraint on its own line.
The CLI tool reports: Infinite loop detected: Reported diagnostics have been previously fixed.

Metadata

Metadata

Assignees

No one assigned

    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