Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for IForEachLoopOperation in DoNotCopyValue #4368

Merged
merged 1 commit into from
Oct 26, 2020
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
24 changes: 22 additions & 2 deletions src/Roslyn.Diagnostics.Analyzers/Core/DoNotCopyValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,14 @@ bool IsSupportedConversion()
return false;
}

if (Acquire(operation.Operand) == RefKind.None)
switch (Acquire(operation.Operand))
{
return true;
case RefKind.None:
case RefKind.Ref when operation.Conversion.IsIdentity:
return true;

default:
break;
}

return false;
Expand Down Expand Up @@ -540,6 +545,21 @@ public override void VisitForEachLoop(IForEachLoopOperation operation)
CheckLocalSymbolInUnsupportedContext(operation, local);
}

var instance = operation.Collection;
var instance2 = (operation.Collection as IConversionOperation)?.Operand;
if (Acquire(operation.Collection) != RefKind.Ref)
{
instance = null;
instance2 = null;
}
else if (Acquire(instance2) != RefKind.Ref)
{
instance2 = null;
}

using var releaser = TryAddForVisit(_handledOperations, instance, out _);
using var releaser2 = TryAddForVisit(_handledOperations, instance2, out _);

CheckTypeInUnsupportedContext(operation);
base.VisitForEachLoop(operation);
}
Expand Down
119 changes: 119 additions & 0 deletions src/Roslyn.Diagnostics.Analyzers/UnitTests/DoNotCopyValueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,125 @@ struct CannotCopy
public int Second { get; set; }
}

internal sealed class NonCopyableAttribute : System.Attribute { }
";

await new VerifyCS.Test
{
TestCode = source,
LanguageVersion = Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8,
}.RunAsync();
}

[Fact]
public async Task AllowCustomForeachEnumerator()
{
var source = @"
using System.Runtime.InteropServices;

class C
{
void Method()
{
var cannotCopy = new CannotCopy();
foreach (var obj in cannotCopy)
{
}
}
}

[NonCopyable]
struct CannotCopy
{
public Enumerator GetEnumerator() => throw null;

public struct Enumerator
{
public object Current => throw null;
public bool MoveNext() => throw null;
}
}

internal sealed class NonCopyableAttribute : System.Attribute { }
";

await new VerifyCS.Test
{
TestCode = source,
LanguageVersion = Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8,
}.RunAsync();
}

[Fact]
public async Task AllowCustomForeachEnumeratorDisposableObject()
{
var source = @"
using System;
using System.Runtime.InteropServices;

class C
{
void Method()
{
using var cannotCopy = new CannotCopy();
foreach (var obj in cannotCopy)
{
}
}
}

[NonCopyable]
struct CannotCopy : IDisposable
{
public void Dispose() => throw null;
public Enumerator GetEnumerator() => throw null;

public struct Enumerator
{
public object Current => throw null;
public bool MoveNext() => throw null;
}
}

internal sealed class NonCopyableAttribute : System.Attribute { }
";

await new VerifyCS.Test
{
TestCode = source,
LanguageVersion = Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp8,
}.RunAsync();
}

[Fact]
public async Task AllowCustomForeachReadonlyEnumerator()
{
var source = @"
using System.Runtime.InteropServices;

class C
{
void Method()
{
var cannotCopy = new CannotCopy();
foreach (var obj in cannotCopy)
{
}
}
}

[NonCopyable]
struct CannotCopy
{
public readonly Enumerator GetEnumerator() => throw null;

public struct Enumerator
{
public object Current => throw null;
public bool MoveNext() => throw null;
}
}

internal sealed class NonCopyableAttribute : System.Attribute { }
";

Expand Down