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

Ensure that we register to analyze generated code for MakeFieldReadO… #52726

Merged
merged 2 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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,12 @@ protected static DiagnosticDescriptor CreateDescriptorWithId(
customTags: DiagnosticCustomTags.Create(isUnnecessary, isConfigurable, enforceOnBuild));
#pragma warning restore RS0030 // Do not used banned APIs

// Code style analyzers should not run on generated code, unless explicitly required by the analyzer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better as a doc comment. Probably also extend the statement to say it should either be None or Analyze, but not ReportDiagnostic?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably also extend the statement to say it should either be None or Analyze, but not ReportDiagnostic?

Hmm, I did not want to be so pedantic given we may have some analyzer that also wants to report on generated code? I can actually make this simple by just making this a bool property though, which may make the purpose more explicit.

protected virtual GeneratedCodeAnalysisFlags GeneratedCodeAnalysisFlags => GeneratedCodeAnalysisFlags.None;

public sealed override void Initialize(AnalysisContext context)
{
// Code style analyzers should not run on generated code.
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags);
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 GeneratedCodeAnalysisFlags GeneratedCodeAnalysisFlags => GeneratedCodeAnalysisFlags.Analyze;

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