Skip to content

Fix S4144 FP: when type constraints are used #7068

@costin-zaharia-sonarsource

Description

Description

Given two methods with identical code and a single type parameter T; and, given different type constraints, a false S4144 (“Update this method so that its implementation is not identical to …”) message is generated.

Source: https://community.sonarsource.com/t/false-s4144-when-otherwise-identical-generic-methods-have-different-type-constraints/88443

Repro steps

public static class TypeConstraints
{
    public static int Use<T>(T? value) where T : struct => 1;

    public static int Use<T>(T? value) where T : class => 2;

    public static void First<T>(T? value) where T : struct // Secondary
    {
        var x = Use(value);
        Console.WriteLine(x);
    }

    public static void Second<T>(T? value) where T : class // Noncompliant
    {
        var x = Use(value);
        Console.WriteLine(x);
    }
}

Expected behavior

We should not raise any issues here. A different overload is used due to the type constraints.

Actual behavior

An issue is raised. See the code snippet.

Related information

  • C#/VB.NET Plugins version: 8.55

Metadata

Metadata

Labels

False PositiveRule IS triggered when it shouldn't be.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions