dotnet / dotnet/linker

Some warnings are hidden due to constant propagation

Open
#2,864 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

```csharp
using DAM = System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute;
using DAMT = System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes;
using RUC = System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute;

class Program {
public static void Main() {
RequireAll(GetMethods()); // No warning!
}

[return: DAM(DAMT.PublicMethods)]
static Type GetMethods() => null;

static void RequireAll([DAM(DAMT.All)] Type t) {}
}
```

Trim this with `link`:
```xml


Exe
net7.0
enable
enable
true
true
win-x64
false
link

```

And it doesn't produce any warnings. The reason seems to be that constant propagation sees the `null` return in `GetMethods` and inlines the constant. The warning shows up if you change `null` to `throw null`.

Technically this is ok since there's no behavior difference with trimming, but it certainly surprised me. Probably not that important since the analyzer will catch this and most similar cases (plus, people are unlikely to use null like this).

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.