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

Add config parameter for value task consumption #7459

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
Expand Down Expand Up @@ -131,6 +132,16 @@ public sealed override void Initialize(AnalysisContext context)
// ConfigureAwait returns another awaitable. Use that one instead for subsequent analysis.
operation = invocation = parentIo;
break;
default:
var additionalMethods =
operationContext.Options.GetStringOptionValue(
EditorConfigOptionNames.AdditionalValidValueTaskConsumption, GeneralRule,
operation.Syntax.SyntaxTree, operationContext.Compilation)
.Split(['|'], StringSplitOptions.RemoveEmptyEntries)
.ToImmutableArray();
if (additionalMethods.Contains(parentIo.TargetMethod.Name))
return;
break;
}
}
else if (invocation.Parent is IPropertyReferenceOperation { Property.Name: nameof(ValueTask<int>.Result) })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Threading.Tasks;
Expand Down Expand Up @@ -1371,6 +1371,41 @@ public async Task MultipleAwaitsAcrossDifferingConditions<T>(bool condition1, bo
);
}

[Theory]
[InlineData("dotnet_code_quality.additional_valid_valuetask_consumption= KeepContext")]
public async Task NoDiagnostics_AdditionalMembers_VBAsync(string editorConfigText)
{
var test = new VerifyVB.Test
{
TestState =
{
Sources =
{
VBBoilerplate("""
Imports System
Imports System.Threading.Tasks

Class C
Public Async Sub DontConsume()
Await Helpers.ReturnsValueTask().KeepContext()
End Sub
End Class
Module AsyncHelpers
<System.Runtime.CompilerServices.Extension>
Public Function KeepContext(vt As ValueTask) As System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable
Return vt.ConfigureAwait(True)
End Function
End Module
""")
},
AnalyzerConfigFiles = { ("/.editorconfig", $@"root = true
[*]
{editorConfigText}") }
}
};
await test.RunAsync();
}

#endregion

private static DiagnosticResult GetCSharpResultAt(int line, int column, DiagnosticDescriptor rule) =>
Expand Down
5 changes: 5 additions & 0 deletions src/Utilities/Compiler/Options/EditorConfigOptionNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,10 @@ internal static partial class EditorConfigOptionNames
/// Boolean option whether to perform the analysis even if the assembly exposes its internals.
/// </summary>
public const string IgnoreInternalsVisibleTo = "ignore_internalsvisibleto";

/// <summary>
/// String option to configure names of additional safe value task consumptions (separated by '|') for CA2012.
/// </summary>
public const string AdditionalValidValueTaskConsumption = "additional_valid_valuetask_consumption";
}
}