icsharpcode / ILSpy

.NET Decompiler with support for PDB generation, ReadyToRun, Metadata (&more) - cross-platform!
21.34k stars 3.34k forks source link

Const string reference replaced by string literal value #3222

Open Aussiemon opened 3 months ago

Aussiemon commented 3 months ago

Similar to #1084.

The reference to const kPlantableNameKey is replaced by its value in the constructor, which is valid code, but an unused assignment / variable warning at the IDE. Perhaps the decompiler could check for private constants and replace matching literals in scope, or perhaps this is a more specific edge case.

Input code

public class TestDecomp
{
    private const string kPlantableNameKey = "Chamomile";

    public string GetPlantableNameKey()
    {
        return kPlantableNameKey;
    }
}

Erroneous output (from .NET 8.0 SDK class library)

public class TestDecomp
{
    private const string kPlantableNameKey = "Chamomile";

    public string GetPlantableNameKey()
    {
        return "Chamomile";
    }
}

Details

siegfriedpammer commented 1 month ago

This is not a bug, but a request for improvement. While we do some prettifying of double and float constants, that can be expressed as fractions or multiples of Math.PI or E, I don't think this case is worth the effort.

Questions:

  1. Where should we look for constants? Current type, current assembly or all referenced assemblies?
  2. Should we look at possible sub strings?

These two questions are examples of why such a feature is problematic in my opinion.