dotnet / dotnet/sdk

ConstantExpectedAnalyzer CA1857 does not trigger on constructor calls

Open
#54,740 1 comment 0 reactions 0 assignees View on GitHub
Area-Microsoft.CodeAnalysis.NetAnalyzers untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### 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:

```cs
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

```console
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.

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.