Skip to content

Commit ab446be

Browse files
committed
Apply PR comments
1 parent 231edb2 commit ab446be

File tree

6 files changed

+31
-87
lines changed

6 files changed

+31
-87
lines changed

src/NetAnalyzers/CSharp/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/CSharpDoNotDirectlyAwaitATask.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotDirectlyAwaitATask.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ namespace Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines
1616
/// <summary>
1717
/// CA2007: <inheritdoc cref="DoNotDirectlyAwaitATaskTitle"/>
1818
/// </summary>
19-
public abstract class DoNotDirectlyAwaitATaskAnalyzer : DiagnosticAnalyzer
19+
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
20+
public sealed class DoNotDirectlyAwaitATaskAnalyzer : DiagnosticAnalyzer
2021
{
2122
internal const string RuleId = "CA2007";
2223

@@ -81,17 +82,20 @@ public override void Initialize(AnalysisContext context)
8182

8283
if (configuredAsyncEnumerable is not null)
8384
{
84-
RegisterLanguageSpecificChecks(context, configuredAsyncEnumerable);
85+
context.RegisterOperationAction(ctx => AnalyzeAwaitForEachLoopOperation(ctx, configuredAsyncEnumerable), OperationKind.Loop);
8586
}
8687
}
8788
});
8889
});
8990
}
9091

91-
#pragma warning disable RS1012
92-
protected virtual void RegisterLanguageSpecificChecks(OperationBlockStartAnalysisContext context, INamedTypeSymbol configuredAsyncEnumerable)
93-
#pragma warning restore RS1012
92+
private static void AnalyzeAwaitForEachLoopOperation(OperationAnalysisContext context, INamedTypeSymbol configuredAsyncEnumerable)
9493
{
94+
if (context.Operation is IForEachLoopOperation { IsAsynchronous: true, Collection.Type: not null } forEachOperation
95+
&& !forEachOperation.Collection.Type.OriginalDefinition.Equals(configuredAsyncEnumerable, SymbolEqualityComparer.Default))
96+
{
97+
context.ReportDiagnostic(forEachOperation.Collection.CreateDiagnostic(Rule));
98+
}
9599
}
96100

97101
private static void AnalyzeAwaitOperation(OperationAnalysisContext context, ImmutableArray<INamedTypeSymbol> taskTypes)

src/NetAnalyzers/Microsoft.CodeAnalysis.NetAnalyzers.sarif

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -436,25 +436,6 @@
436436
]
437437
}
438438
},
439-
"CA2007": {
440-
"id": "CA2007",
441-
"shortDescription": "Consider calling ConfigureAwait on the awaited task",
442-
"fullDescription": "When an asynchronous method awaits a Task directly, continuation occurs in the same thread that created the task. Consider calling Task.ConfigureAwait(Boolean) to signal your intention for continuation. Call ConfigureAwait(false) on the task to schedule continuations to the thread pool, thereby avoiding a deadlock on the UI thread. Passing false is a good option for app-independent libraries. Calling ConfigureAwait(true) on the task has the same behavior as not explicitly calling ConfigureAwait. By explicitly calling this method, you're letting readers know you intentionally want to perform the continuation on the original synchronization context.",
443-
"defaultLevel": "warning",
444-
"helpUri": "https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007",
445-
"properties": {
446-
"category": "Reliability",
447-
"isEnabledByDefault": false,
448-
"typeName": "CSharpDoNotDirectlyAwaitATask",
449-
"languages": [
450-
"C#"
451-
],
452-
"tags": [
453-
"Telemetry",
454-
"EnabledRuleInAggressiveMode"
455-
]
456-
}
457-
},
458439
"CA2014": {
459440
"id": "CA2014",
460441
"shortDescription": "Do not use stackalloc in loops",
@@ -3431,6 +3412,26 @@
34313412
]
34323413
}
34333414
},
3415+
"CA2007": {
3416+
"id": "CA2007",
3417+
"shortDescription": "Consider calling ConfigureAwait on the awaited task",
3418+
"fullDescription": "When an asynchronous method awaits a Task directly, continuation occurs in the same thread that created the task. Consider calling Task.ConfigureAwait(Boolean) to signal your intention for continuation. Call ConfigureAwait(false) on the task to schedule continuations to the thread pool, thereby avoiding a deadlock on the UI thread. Passing false is a good option for app-independent libraries. Calling ConfigureAwait(true) on the task has the same behavior as not explicitly calling ConfigureAwait. By explicitly calling this method, you're letting readers know you intentionally want to perform the continuation on the original synchronization context.",
3419+
"defaultLevel": "warning",
3420+
"helpUri": "https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007",
3421+
"properties": {
3422+
"category": "Reliability",
3423+
"isEnabledByDefault": false,
3424+
"typeName": "DoNotDirectlyAwaitATaskAnalyzer",
3425+
"languages": [
3426+
"C#",
3427+
"Visual Basic"
3428+
],
3429+
"tags": [
3430+
"Telemetry",
3431+
"EnabledRuleInAggressiveMode"
3432+
]
3433+
}
3434+
},
34343435
"CA2008": {
34353436
"id": "CA2008",
34363437
"shortDescription": "Do not create tasks without passing a TaskScheduler",
@@ -6599,25 +6600,6 @@
65996600
]
66006601
}
66016602
},
6602-
"CA2007": {
6603-
"id": "CA2007",
6604-
"shortDescription": "Consider calling ConfigureAwait on the awaited task",
6605-
"fullDescription": "When an asynchronous method awaits a Task directly, continuation occurs in the same thread that created the task. Consider calling Task.ConfigureAwait(Boolean) to signal your intention for continuation. Call ConfigureAwait(false) on the task to schedule continuations to the thread pool, thereby avoiding a deadlock on the UI thread. Passing false is a good option for app-independent libraries. Calling ConfigureAwait(true) on the task has the same behavior as not explicitly calling ConfigureAwait. By explicitly calling this method, you're letting readers know you intentionally want to perform the continuation on the original synchronization context.",
6606-
"defaultLevel": "warning",
6607-
"helpUri": "https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2007",
6608-
"properties": {
6609-
"category": "Reliability",
6610-
"isEnabledByDefault": false,
6611-
"typeName": "BasicDoNotDirectlyAwaitATask",
6612-
"languages": [
6613-
"Visual Basic"
6614-
],
6615-
"tags": [
6616-
"Telemetry",
6617-
"EnabledRuleInAggressiveMode"
6618-
]
6619-
}
6620-
},
66216603
"CA2016": {
66226604
"id": "CA2016",
66236605
"shortDescription": "Forward the 'CancellationToken' parameter to methods",

src/NetAnalyzers/RulesMissingDocumentation.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ CA1510 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-
66
CA1511 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1511> | Use ArgumentException throw helper |
77
CA1512 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1512> | Use ArgumentOutOfRangeException throw helper |
88
CA1513 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1513> | Use ObjectDisposedException throw helper |
9-
CA1514 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1514> | Avoid redundant length argument |
109
CA1515 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1515> | Consider making public types internal |
1110
CA1856 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1856> | Incorrect usage of ConstantExpected attribute |
1211
CA1857 | <https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1857> | A constant is expected for the parameter |

src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotDirectlyAwaitATaskTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
using Xunit;
88
using Microsoft.CodeAnalysis.CSharp;
99
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
10-
Microsoft.CodeQuality.CSharp.Analyzers.ApiDesignGuidelines.CSharpDoNotDirectlyAwaitATask,
10+
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotDirectlyAwaitATaskAnalyzer,
1111
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotDirectlyAwaitATaskFixer>;
1212
using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier<
13-
Microsoft.CodeQuality.VisualBasic.Analyzers.ApiDesignGuidelines.BasicDoNotDirectlyAwaitATask,
13+
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotDirectlyAwaitATaskAnalyzer,
1414
Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.DoNotDirectlyAwaitATaskFixer>;
1515

1616
namespace Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.UnitTests

src/NetAnalyzers/VisualBasic/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/BasicDoNotDirectlyAwaitATask.vb

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)