Skip to content
This repository was archived by the owner on Apr 20, 2026. It is now read-only.

Commit f3c78df

Browse files
committed
Fixed AV1500: Do not report on captured variables from primary constructor initializer
1 parent 83d6974 commit f3c78df

6 files changed

Lines changed: 60 additions & 11 deletions

File tree

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/Specs/Maintainability/AvoidMemberWithManyStatementsSpecs.Nested.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,36 @@ void M()
223223
// Act and assert
224224
await VerifyGuidelineDiagnosticAsync(source);
225225
}
226+
227+
[Fact]
228+
internal async Task When_type_has_primary_constructor_with_base_initializer_it_must_be_skipped()
229+
{
230+
// Arrange
231+
ParsedSourceCode source = new TypeSourceCodeBuilder()
232+
.InGlobalScope("""
233+
class C(string s) : B(s)
234+
{
235+
void M1()
236+
{
237+
; ; ; ;
238+
}
239+
240+
void M2()
241+
{
242+
; ; ; ;
243+
}
244+
}
245+
246+
abstract class B
247+
{
248+
protected B(string s)
249+
{
250+
}
251+
}
252+
""")
253+
.Build();
254+
255+
// Act and assert
256+
await VerifyGuidelineDiagnosticAsync(source);
257+
}
226258
}

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer.Test/TestDataBuilders/MemberSourceCodeBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace CSharpGuidelinesAnalyzer.Test.TestDataBuilders;
44

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

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Maintainability/AvoidMemberWithManyStatementsAnalyzer.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,15 @@ private static void RegisterCompilationStart([NotNull] CompilationStartAnalysisC
5656
startContext.RegisterCodeBlockAction(actionContext => AnalyzeCodeBlock(actionContext, settingsReader));
5757
}
5858

59-
private static int GetMaxStatementCountFromSettings([NotNull] AnalyzerSettingsReader settingsReader, [NotNull] SyntaxTree syntaxTree)
60-
{
61-
Guard.NotNull(settingsReader, nameof(settingsReader));
62-
Guard.NotNull(syntaxTree, nameof(syntaxTree));
63-
64-
return settingsReader.TryGetInt32(syntaxTree, MaxStatementCountKey, 0, 255) ?? DefaultMaxStatementCount;
65-
}
66-
6759
private static void AnalyzeCodeBlock(CodeBlockAnalysisContext context, [NotNull] AnalyzerSettingsReader settingsReader)
6860
{
69-
int maxStatementCount = GetMaxStatementCountFromSettings(settingsReader, context.CodeBlock.SyntaxTree);
70-
71-
if (context.OwningSymbol is INamedTypeSymbol || context.OwningSymbol.IsSynthesized())
61+
if (context.OwningSymbol is INamedTypeSymbol || context.OwningSymbol.IsSynthesized() || IsPrimaryConstructorInitializer(context.CodeBlock))
7262
{
7363
return;
7464
}
7565

66+
int maxStatementCount = GetMaxStatementCountFromSettings(settingsReader, context.CodeBlock.SyntaxTree);
67+
7668
var statementWalker = new StatementWalker(context.CancellationToken);
7769
statementWalker.Visit(context.CodeBlock);
7870

@@ -82,6 +74,19 @@ private static void AnalyzeCodeBlock(CodeBlockAnalysisContext context, [NotNull]
8274
}
8375
}
8476

77+
private static bool IsPrimaryConstructorInitializer([NotNull] SyntaxNode codeBlock)
78+
{
79+
return codeBlock is BaseTypeDeclarationSyntax;
80+
}
81+
82+
private static int GetMaxStatementCountFromSettings([NotNull] AnalyzerSettingsReader settingsReader, [NotNull] SyntaxTree syntaxTree)
83+
{
84+
Guard.NotNull(settingsReader, nameof(settingsReader));
85+
Guard.NotNull(syntaxTree, nameof(syntaxTree));
86+
87+
return settingsReader.TryGetInt32(syntaxTree, MaxStatementCountKey, 0, 255) ?? DefaultMaxStatementCount;
88+
}
89+
8590
private static void ReportAtContainingSymbol(int statementCount, int maxStatementCount, CodeBlockAnalysisContext context)
8691
{
8792
string kind = GetMemberKind(context.OwningSymbol, context.CancellationToken);

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Maintainability/IfElseIfConstructShouldFinishWithElseClauseAnalyzer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ private bool IsIfElseIfConstruct([NotNull] IConditionalOperation ifStatement)
145145
return ifStatement.WhenFalse is IConditionalOperation;
146146
}
147147

148+
// Workaround for https://github.com/bkoelman/CSharpGuidelinesAnalyzer/issues/155.
149+
#pragma warning disable AV1500 // Member or local function contains too many statements
148150
private sealed class IfElseIfConstructAnalyzer([NotNull] IfStatementAnalyzer owner, [NotNull] IConditionalOperation ifStatement)
151+
#pragma warning restore AV1500 // Member or local function contains too many statements
149152
{
150153
[NotNull]
151154
private readonly IfStatementAnalyzer owner = owner;

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Rules/Maintainability/OverloadShouldCallOtherOverloadAnalyzer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,10 @@ private static bool InvokesAnotherOverload([NotNull] IMethodSymbol methodToAnaly
264264
return false;
265265
}
266266

267+
// Workaround for https://github.com/bkoelman/CSharpGuidelinesAnalyzer/issues/155.
268+
#pragma warning disable AV1500 // Member or local function contains too many statements
267269
private sealed class MethodInvocationWalker([NotNull] [ItemNotNull] IReadOnlyCollection<IMethodSymbol> methodGroup) : ExplicitOperationWalker
270+
#pragma warning restore AV1500 // Member or local function contains too many statements
268271
{
269272
[NotNull]
270273
[ItemNotNull]

src/CSharpGuidelinesAnalyzer/CSharpGuidelinesAnalyzer/Settings/AnalyzerSettingsReader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
namespace CSharpGuidelinesAnalyzer.Settings;
99

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

0 commit comments

Comments
 (0)