Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ private void HandleOperation(ISymbol symbol, IOperation operation)
if (!IsPotentialSymbol(symbol))
return;

// Assignment targets (e.g., initializations in constructors) are not usages
if (operation.Parent is IAssignmentOperation { Target: var assignTarget } && assignTarget == operation)
return;

if (operation.Parent is not ILockOperation)
{
ExcludeSymbol(symbol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,99 @@ void A()
""")
.ValidateAsync();
}

[Fact]
public async Task Field_InitializedInConstructor_OnlyLockUsage()
{
await CreateProjectBuilder()
.WithSourceCode("""
public sealed class A
{
private readonly object [||]_lock;

public A()
{
_lock = new object();
}

public void Run()
{
lock (_lock) { }
}
}
""")
.ValidateAsync();
}

[Fact]
public async Task Field_InitializedInConstructor_LockAndOtherUsages()
{
await CreateProjectBuilder()
.WithSourceCode("""
public sealed class A
{
private readonly object _lock;

public A()
{
_lock = new object();
}

public void Run()
{
lock (_lock) { }
_lock.ToString();
}
}
""")
.ValidateAsync();
}

[Fact]
public async Task StaticField_InitializedInStaticConstructor_OnlyLockUsage()
{
await CreateProjectBuilder()
.WithSourceCode("""
public sealed class B
{
private static readonly object [||]Lock;

static B()
{
Lock = new object();
}

public void Run()
{
lock (Lock) { }
}
}
""")
.ValidateAsync();
}

[Fact]
public async Task StaticField_InitializedInStaticConstructor_LockAndOtherUsages()
{
await CreateProjectBuilder()
.WithSourceCode("""
public sealed class B
{
private static readonly object Lock;

static B()
{
Lock = new object();
}

public void Run()
{
lock (Lock) { }
Lock.ToString();
}
}
""")
.ValidateAsync();
}
#endif
}