Skip to content

ConstantExpectedAnalyzer CA1857 does not trigger on constructor calls #54740

@jafaber

Description

@jafaber

Describe the bug

Decorating a constructor parameter with [ConstantExpected] does nothing. The ConstantExpectedAnalyzer basically ignores it.

At least in the documentation I've seen, this does not seem to be deliberate.

Steps to reproduce

Minimal code example that shows the problem:

internal class C
{
    private C([ConstantExpected(Min = 3, Max = 4)] int value) { }
    private static void M([ConstantExpected(Min = 3, Max = 4)] int value) { }

    private static void A()
    {
        M(0); // Correctly triggers: CA1857: The constant does not fit within the value bounds of '3' to '4'
        var obj = new C(0); // BUG: Does NOT trigger CA1857
    }
}

Expected behavior

CA1857: The constant does not fit within the value bounds of '3' to '4' on both calls.

Actual behavior

Only normal method call triggers CA1857: The constant does not fit within the value bounds of '3' to '4', the call to the constructor C does not.

Is this a regression?

Don't think so.

Are there any workarounds?

Don't think so.

dotnet --info output

C:\Windows\System32>dotnet --info
.NET SDK:
 Version:           10.0.301
 Commit:            96856fd726
 Workload version:  10.0.300-manifests.8c7d7c03
 MSBuild version:   18.6.4+96856fd72

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.26200
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\10.0.301\

.NET workloads installed:
There are no installed workloads to display.
Configured to use workload sets when installing new manifests.
No workload sets are installed. Run "dotnet workload restore" to install a workload set.

Host:
  Version:      10.0.9
  Architecture: x64
  Commit:       901ca94124

.NET SDKs installed:
  10.0.100-rc.1.25451.107 [C:\Program Files\dotnet\sdk]
  10.0.301 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 10.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 10.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 9.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 10.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

IDE version

VS 18.7.0

Other details

AI is saying that the below Invocation registration (as found in current analyzer source code) does not cover constructor calls. For that it suggests adding a similar registration for OperationKind.ObjectCreation. I have not (yet) investigated this any further.

context.RegisterOperationAction(context => OnInvocation(context, constantExpectedContext), OperationKind.Invocation);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions