Skip to content

Commit

Permalink
Merge pull request #52726 from mavasani/FixGeneratedCodeIssue
Browse files Browse the repository at this point in the history
Ensure that we register to analyze generated code for MakeFieldReadO…
  • Loading branch information
mavasani authored Apr 19, 2021
2 parents 4b1cd21 + de17f29 commit 3fd808a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1746,5 +1746,38 @@ class Program
private static object [|t_obj|];
}");
}

[WorkItem(50925, "https://github.com/dotnet/roslyn/issues/50925")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeFieldReadonly)]
public async Task Test_MemberUsedInGeneratedCode()
{
await TestMissingInRegularAndScriptAsync(
@"<Workspace>
<Project Language = ""C#"" AssemblyName=""Assembly1"" CommonReferences=""true"">
<Document FilePath = ""z:\\File1.cs"">
public sealed partial class Test
{
private int [|_value|];
public static void M()
=> _ = new Test { Value = 1 };
}
</Document>
<Document FilePath = ""z:\\File2.g.cs"">
using System.CodeDom.Compiler;
[GeneratedCode(null, null)]
public sealed partial class Test
{
public int Value
{
get => _value;
set => _value = value;
}
}
</Document>
</Project>
</Workspace>");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ protected static DiagnosticDescriptor CreateDescriptorWithId(
customTags: DiagnosticCustomTags.Create(isUnnecessary, isConfigurable, enforceOnBuild));
#pragma warning restore RS0030 // Do not used banned APIs

/// <summary>
/// Flag indicating whether or not analyzer should receive analysis callbacks for generated code.
/// By default, code style analyzers should not run on generated code, so the value is false.
/// </summary>
protected virtual bool ReceiveAnalysisCallbacksForGeneratedCode => false;

public sealed override void Initialize(AnalysisContext context)
{
// Code style analyzers should not run on generated code.
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
var flags = ReceiveAnalysisCallbacksForGeneratedCode ? GeneratedCodeAnalysisFlags.Analyze : GeneratedCodeAnalysisFlags.None;
context.ConfigureGeneratedCodeAnalysis(flags);
context.EnableConcurrentExecution();

InitializeWorker(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public MakeFieldReadonlyDiagnosticAnalyzer()

public override DiagnosticAnalyzerCategory GetAnalyzerCategory() => DiagnosticAnalyzerCategory.SemanticDocumentAnalysis;

// We need to analyze generated code to get callbacks for read/writes to non-generated members in generated code.
protected override bool ReceiveAnalysisCallbacksForGeneratedCode => true;

protected override void InitializeWorker(AnalysisContext context)
{
context.RegisterCompilationStartAction(compilationStartContext =>
Expand Down

0 comments on commit 3fd808a

Please sign in to comment.