Files
ILSpy/ILSpy.Tests/Analyzers/Library/TestCases/MainAssembly.cs
T
Siegfried Pammer 7572b57eee Fix #2372: report a field's address loads on their own
Loading a field's address was counted as both a read and a write, so a
use that only reads - calling a method on a value-type field emits
ldflda and writes nothing - was listed under "Assigned By".

An address load says only that something needed a reference to the
field. What happens through that reference is up to the consumer, and
the IL scan here does not look at it, so the load is neither a read nor
a write and gets a category of its own. Special-casing the methods known
not to mutate their receiver would answer this one screenshot and leave
every ref argument still guessing.

Assisted-by: Claude:claude-opus-5:Claude Code
2026-09-07 23:00:53 +02:00

73 lines
2.3 KiB
C#

// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
namespace ICSharpCode.ILSpy.Tests.Analyzers.Library.TestCases.Main
{
// Fixture for the analyzer-library tests. Methods are picked up by metadata-token
// lookup from the test assembly itself, so each one's IL must reach the operation
// the analyser is meant to find — that's why the bodies look trivial.
class MainAssembly
{
public string UsesSystemStringEmpty()
{
return string.Empty;
}
public int UsesInt32()
{
return int.Parse("1234");
}
}
// Fixture for the field-access analysers. A field of a value type reached through a
// method call is loaded by address (ldflda/ldsflda), which says nothing about whether
// the call writes to it - issue #2372.
class FieldAccess
{
public bool instanceFlag;
public static bool staticFlag;
public string ReadsInstanceFlagByAddress()
{
// callvirt Boolean::ToString(ldflda instanceFlag)
return instanceFlag.ToString();
}
public static string ReadsStaticFlagByAddress()
{
// call Boolean::ToString(ldsflda staticFlag)
return staticFlag.ToString();
}
public bool ReadsInstanceFlag()
{
return instanceFlag;
}
public void WritesInstanceFlag()
{
instanceFlag = true;
}
public static void WritesStaticFlag()
{
staticFlag = true;
}
}
}