Skip to content

Commit

Permalink
Fixed AV1500: Do not report on captured variables from primary constr…
Browse files Browse the repository at this point in the history
…uctor initializer
  • Loading branch information
bkoelman committed Apr 20, 2024
1 parent 83d6974 commit f3c78df
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,36 @@ void M()
// Act and assert
await VerifyGuidelineDiagnosticAsync(source);
}

[Fact]
internal async Task When_type_has_primary_constructor_with_base_initializer_it_must_be_skipped()
{
// Arrange
ParsedSourceCode source = new TypeSourceCodeBuilder()
.InGlobalScope("""
class C(string s) : B(s)
{
void M1()
{
; ; ; ;
}

void M2()
{
; ; ; ;
}
}

abstract class B
{
protected B(string s)
{
}
}
""")
.Build();

// Act and assert
await VerifyGuidelineDiagnosticAsync(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace CSharpGuidelinesAnalyzer.Test.TestDataBuilders;

/// <summary />
// Workaround for https://github.com/bkoelman/CSharpGuidelinesAnalyzer/issues/155.
#pragma warning disable AV1500 // Member or local function contains too many statements
internal sealed class MemberSourceCodeBuilder() : SourceCodeBuilder(DefaultNamespaceImports)
#pragma warning restore AV1500 // Member or local function contains too many statements
{
private readonly List<string> members = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,15 @@ private static void RegisterCompilationStart([NotNull] CompilationStartAnalysisC
startContext.RegisterCodeBlockAction(actionContext => AnalyzeCodeBlock(actionContext, settingsReader));
}

private static int GetMaxStatementCountFromSettings([NotNull] AnalyzerSettingsReader settingsReader, [NotNull] SyntaxTree syntaxTree)
{
Guard.NotNull(settingsReader, nameof(settingsReader));
Guard.NotNull(syntaxTree, nameof(syntaxTree));

return settingsReader.TryGetInt32(syntaxTree, MaxStatementCountKey, 0, 255) ?? DefaultMaxStatementCount;
}

private static void AnalyzeCodeBlock(CodeBlockAnalysisContext context, [NotNull] AnalyzerSettingsReader settingsReader)
{
int maxStatementCount = GetMaxStatementCountFromSettings(settingsReader, context.CodeBlock.SyntaxTree);

if (context.OwningSymbol is INamedTypeSymbol || context.OwningSymbol.IsSynthesized())
if (context.OwningSymbol is INamedTypeSymbol || context.OwningSymbol.IsSynthesized() || IsPrimaryConstructorInitializer(context.CodeBlock))
{
return;
}

int maxStatementCount = GetMaxStatementCountFromSettings(settingsReader, context.CodeBlock.SyntaxTree);

var statementWalker = new StatementWalker(context.CancellationToken);
statementWalker.Visit(context.CodeBlock);

Expand All @@ -82,6 +74,19 @@ private static void AnalyzeCodeBlock(CodeBlockAnalysisContext context, [NotNull]
}
}

private static bool IsPrimaryConstructorInitializer([NotNull] SyntaxNode codeBlock)
{
return codeBlock is BaseTypeDeclarationSyntax;
}

private static int GetMaxStatementCountFromSettings([NotNull] AnalyzerSettingsReader settingsReader, [NotNull] SyntaxTree syntaxTree)
{
Guard.NotNull(settingsReader, nameof(settingsReader));
Guard.NotNull(syntaxTree, nameof(syntaxTree));

return settingsReader.TryGetInt32(syntaxTree, MaxStatementCountKey, 0, 255) ?? DefaultMaxStatementCount;
}

private static void ReportAtContainingSymbol(int statementCount, int maxStatementCount, CodeBlockAnalysisContext context)
{
string kind = GetMemberKind(context.OwningSymbol, context.CancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ private bool IsIfElseIfConstruct([NotNull] IConditionalOperation ifStatement)
return ifStatement.WhenFalse is IConditionalOperation;
}

// Workaround for https://github.com/bkoelman/CSharpGuidelinesAnalyzer/issues/155.
#pragma warning disable AV1500 // Member or local function contains too many statements
private sealed class IfElseIfConstructAnalyzer([NotNull] IfStatementAnalyzer owner, [NotNull] IConditionalOperation ifStatement)
#pragma warning restore AV1500 // Member or local function contains too many statements
{
[NotNull]
private readonly IfStatementAnalyzer owner = owner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ private static bool IsParameterWithDefaultValue([NotNull] IParameterSymbol param
return false;
}

// Workaround for https://github.com/bkoelman/CSharpGuidelinesAnalyzer/issues/155.
#pragma warning disable AV1500 // Member or local function contains too many statements
private sealed class MethodInvocationWalker([NotNull] [ItemNotNull] IReadOnlyCollection<IMethodSymbol> methodGroup) : ExplicitOperationWalker
#pragma warning restore AV1500 // Member or local function contains too many statements
{
[NotNull]
[ItemNotNull]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

namespace CSharpGuidelinesAnalyzer.Settings;

// Workaround for https://github.com/bkoelman/CSharpGuidelinesAnalyzer/issues/155.
#pragma warning disable AV1500 // Member or local function contains too many statements
internal sealed class AnalyzerSettingsReader([NotNull] AnalyzerOptions options, CancellationToken cancellationToken)
#pragma warning restore AV1500 // Member or local function contains too many statements
{
private const string EditorConfigFileName = ".editorconfig";

Expand Down

0 comments on commit f3c78df

Please sign in to comment.