Skip to content

Commit

Permalink
Centralize and clean up diagnostic IDs (#33415)
Browse files Browse the repository at this point in the history
  • Loading branch information
roji authored Mar 28, 2024
1 parent ce152e3 commit 8487da8
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 25 deletions.
19 changes: 19 additions & 0 deletions src/EFCore.Analyzers/EFDiagnostics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore;

/// <summary>
/// Contains the IDs of diagnostics emitted by EF Core analyzers, [Experimental] and other mechanisms.
/// </summary>
public static class EFDiagnostics
{
public const string InternalUsage = "EF1001";
public const string InterpolatedStringUsageInRawQueries = "EF1002";
public const string SuppressUninitializedDbSetRule = "EFSPR1001";

// Diagnostics for [Experimental]
public const string ExperimentalApi = "EF9001";
public const string ProviderExperimentalApi = "EF9002";
public const string PrecompiledQueryExperimental = "EF9100";
}
6 changes: 2 additions & 4 deletions src/EFCore.Analyzers/InternalUsageDiagnosticAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ namespace Microsoft.EntityFrameworkCore;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InternalUsageDiagnosticAnalyzer : DiagnosticAnalyzer
{
public const string Id = "EF1001";
private static readonly int EFLen = "EntityFrameworkCore".Length;

private static readonly DiagnosticDescriptor Descriptor
// HACK: Work around dotnet/roslyn-analyzers#5890 by not using target-typed new
= new DiagnosticDescriptor(
Id,
= new(
EFDiagnostics.InternalUsage,
title: AnalyzerStrings.InternalUsageTitle,
messageFormat: AnalyzerStrings.InternalUsageMessageFormat,
category: "Usage",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.EntityFrameworkCore;
public sealed class InterpolatedStringUsageInRawQueriesCodeFixProvider : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds
=> ImmutableArray.Create(InterpolatedStringUsageInRawQueriesDiagnosticAnalyzer.Id);
=> ImmutableArray.Create(EFDiagnostics.InterpolatedStringUsageInRawQueries);

public override FixAllProvider GetFixAllProvider()
=> WellKnownFixAllProviders.BatchFixer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ namespace Microsoft.EntityFrameworkCore;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class InterpolatedStringUsageInRawQueriesDiagnosticAnalyzer : DiagnosticAnalyzer
{
public const string Id = "EF1002";

private static readonly DiagnosticDescriptor Descriptor
// HACK: Work around dotnet/roslyn-analyzers#5890 by not using target-typed new
= new DiagnosticDescriptor(
Id,
= new(
EFDiagnostics.InterpolatedStringUsageInRawQueries,
title: AnalyzerStrings.InterpolatedStringUsageInRawQueriesAnalyzerTitle,
messageFormat: AnalyzerStrings.InterpolatedStringUsageInRawQueriesMessageFormat,
category: "Security",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.EntityFrameworkCore;
public sealed class UninitializedDbSetDiagnosticSuppressor : DiagnosticSuppressor
{
private static readonly SuppressionDescriptor SuppressUninitializedDbSetRule = new(
id: "EFSPR1001",
id: EFDiagnostics.SuppressUninitializedDbSetRule,
suppressedDiagnosticId: "CS8618",
justification: AnalyzerStrings.UninitializedDbSetWarningSuppressionJustification);

Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Design/EFCore.Design.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DevelopmentDependency>true</DevelopmentDependency>
<ImplicitUsings>true</ImplicitUsings>
<NoWarn>EF1003</NoWarn> <!-- Precompiled query is experimental -->
<NoWarn>EF9100</NoWarn> <!-- Precompiled query is experimental -->
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Relational/EFCore.Relational.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<ImplicitUsings>true</ImplicitUsings>
<NoWarn>$(NoWarn);EF1003</NoWarn> <!-- Precomiled query is experimental -->
<NoWarn>$(NoWarn);EF1004</NoWarn> <!-- Experimental provider-facing feature -->
<NoWarn>$(NoWarn);EF9100</NoWarn> <!-- Precomiled query is experimental -->
<NoWarn>$(NoWarn);EF9002</NoWarn> <!-- Experimental provider-facing feature -->
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.EntityFrameworkCore.Query;
/// Represents an expression that is quotable, that is, capable of returning an expression that, when evaluated, would construct an
/// expression identical to this one. Used to generate code for precompiled queries, which reconstructs this expression.
/// </summary>
[Experimental("EF1003")]
[Experimental(EFDiagnostics.PrecompiledQueryExperimental)]
public interface IRelationalQuotableExpression
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Query/ISqlAliasManagerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.EntityFrameworkCore.Query;
/// for more information and examples.
/// </para>
/// </remarks>
[Experimental("EF1004")]
[Experimental(EFDiagnostics.ProviderExperimentalApi)]
public interface ISqlAliasManagerFactory
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.EntityFrameworkCore.Query;
/// <summary>
/// Utilities used for implementing <see cref="IRelationalQuotableExpression" />.
/// </summary>
[Experimental("EF1003")]
[Experimental(EFDiagnostics.PrecompiledQueryExperimental)]
public static class RelationalExpressionQuotingUtilities
{
private static readonly ParameterExpression RelationalModelParameter
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Query/SqlAliasManagerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Microsoft.EntityFrameworkCore.Query;

/// <inheritdoc />
[Experimental("EF1004")]
[Experimental(EFDiagnostics.ProviderExperimentalApi)]
public class SqlAliasManagerFactory : ISqlAliasManagerFactory
{
/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.SqlServer/EFCore.SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>$(PackageTags);SQL Server</PackageTags>
<ImplicitUsings>true</ImplicitUsings>
<NoWarn>EF1003</NoWarn> <!-- Precompiled query is experimental -->
<NoWarn>EF9100</NoWarn> <!-- Precompiled query is experimental -->
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Sqlite.Core/EFCore.Sqlite.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>$(PackageTags);SQLite</PackageTags>
<ImplicitUsings>true</ImplicitUsings>
<NoWarn>EF1003</NoWarn> <!-- Precompiled query is experimental -->
<NoWarn>EF9100</NoWarn> <!-- Precompiled query is experimental -->
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ class MyClass : {|#0:Microsoft.EntityFrameworkCore.Storage.Internal.RawRelationa

await VerifyCS.VerifyAnalyzerAsync(
source,
VerifyCS.Diagnostic(InternalUsageDiagnosticAnalyzer.Id)
VerifyCS.Diagnostic(EFDiagnostics.InternalUsage)
.WithLocation(0)
.WithSeverity(DiagnosticSeverity.Warning)
.WithMessageFormat(AnalyzerStrings.InternalUsageMessageFormat)
.WithArguments("Microsoft.EntityFrameworkCore.Storage.Internal.RawRelationalParameter"),
VerifyCS.Diagnostic(InternalUsageDiagnosticAnalyzer.Id)
VerifyCS.Diagnostic(EFDiagnostics.InternalUsage)
.WithLocation(1)
.WithSeverity(DiagnosticSeverity.Warning)
.WithMessageFormat(AnalyzerStrings.InternalUsageMessageFormat)
Expand All @@ -69,7 +69,7 @@ public Task Implemented_interface()
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;

class {|#0:MyClass|} : IDbSetSource
{
public object Create(DbContext context, Type type) => null;
Expand Down Expand Up @@ -125,7 +125,7 @@ class C
void M()
{
void SomeGenericMethod<T>() {}

{|#0:SomeGenericMethod<Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IStateManager>()|};
}
}
Expand Down Expand Up @@ -224,7 +224,7 @@ public void Main(string[] args) {
private Task VerifySingleInternalUsageAsync(string source, string internalApi)
=> VerifyCS.VerifyAnalyzerAsync(
source,
VerifyCS.Diagnostic(InternalUsageDiagnosticAnalyzer.Id)
VerifyCS.Diagnostic(EFDiagnostics.InternalUsage)
.WithLocation(0)
.WithSeverity(DiagnosticSeverity.Warning)
.WithMessageFormat(AnalyzerStrings.InternalUsageMessageFormat)
Expand Down

0 comments on commit 8487da8

Please sign in to comment.