-
Notifications
You must be signed in to change notification settings - Fork 466
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
Incorrect report of RS1008 (storing per-compilation data) #782
Comments
The code currently looks for outer and inner types of all fields. So fields of type We could ignore inner types for those cases. I am wondering if this exclusion should be more restrictive and only be applicable for readonly fields. Because it could be possible to capture some compilation through a local lambda declaration in which case the message would still make sense. It could be interesting to have 2 messages, one for confident cases and one where we would want the user to review the actual usage. WDYT @sharwell? Full test: [Fact]
[WorkItem(782, "https://github.com/dotnet/roslyn-analyzers/issues/782")]
public void CSharp_ActionWithCompilation_NoDiagnostic()
{
var source = @"
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
class StyleCopSettings { }
[DiagnosticAnalyzer(LanguageNames.CSharp)]
class MyAnalyzer : DiagnosticAnalyzer
{
private static readonly Action<SyntaxTreeAnalysisContext, Compilation, StyleCopSettings> SyntaxTreeAction = HandleSyntaxTree;
private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, Compilation compilation, StyleCopSettings settings)
{
}
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get
{
throw new NotImplementedException();
}
}
public override void Initialize(AnalysisContext context)
{
}
}";
VerifyCSharp(source);
} |
This warning is also reported on nested types that that contain a variable of type INamedTypeSymbol. namespace MyNamespace
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AnyInstanceInjectionAnalyzer : APCodingRulesAnalyzerBase
{
public struct DependencyAccess
{
public IMethodSymbol method;
public string expectedName;
}
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterCompilationStartAction(OnCompilationStart);
}
public void OnCompilationStart(CompilationStartAnalysisContext context)
{
var accessors = new ConcurrentBag<DependencyAccess>();
context.RegisterSymbolAction(
symbolContext => AnalyzeSymbol(symbolContext, accessors),
SymbolKind.Property,
SymbolKind.Field
);
context.RegisterSemanticModelAction(
semanticModelContext => AnalyzeSemanticModel(semanticModelContext, accessors)
);
}
public void AnalyzeSymbol(SymbolAnalysisContext context, ConcurrentBag<DependencyAccess> accessors)
{
// collect symbols for analysis
}
public void AnalyzeSemanticModel(SemanticModelAnalysisContext context, ConcurrentBag<DependencyAccess> accessors)
{
foreach (var access in accessors)
{
// analyze
}
}
}
} |
Also, the warning no longer triggers when I change the |
It should not be, and I reported this bug here #7196 |
I believe the following will reproduce the issue:
The warning is reported in the declaration of
SyntaxTreeAction
, on the tokenCompilation
.The text was updated successfully, but these errors were encountered: